
本篇文章介绍:
如何使用Toolbar;
自定义Toolbar;
先来看一看效果,了解一下toolbar;
布局文件:
<androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="@color/colorPrimary"/>
Actvity中设置属性:
Toolbar toolbar= (Toolbar) findVIEwByID(R.ID.toolbar);toolbar.setlogo(R.mipmap.ic_launcher);//设置图标toolbar.setTitle("Title");//设置主标题toolbar.setSubTitle("smallTitle");//设置子标题这样就可以实现上面的效果。
接下来是自定义的Toolbar:
布局文件:
<com.example.cjj.test.bean.MyToolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="?attr/colorPrimary" androID:minHeight="?attr/actionbarSize" androID:layout_centerInParent="true" androID:layout_gravity="center" > </com.example.cjj.test.bean.MyToolbar>
toolbar.xml:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <Imagebutton androID:ID="@+ID/mleftbutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_centerVertical="true" androID:background="?attr/colorPrimary" /> <TextVIEw androID:ID="@+ID/toolbar_Title" androID:text="Title" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_centerInParent="true" androID:layout_gravity="center" androID:gravity="center" androID:textcolor="@color/white" androID:textSize="20sp" /> <Imagebutton androID:ID="@+ID/mRightbutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:background="?attr/colorPrimary"/></relativeLayout>
新建一个MyToolbar:
public class MyToolbar extends Toolbar { //布局 private LayoutInflater mInflater; //右边按钮 private Imagebutton mRightbutton; //左边按钮 private Imagebutton mleftbutton; //标题 private TextVIEw mTextTitle; private VIEw vIEw; public MyToolbar(Context context) { this(context,null); } public MyToolbar(Context context,AttributeSet attrs) { this(context,attrs,0); } public MyToolbar(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); //初始化函数 initVIEw(); setContentInsetsrelative(10,10); if (attrs != null) { setleftbuttonIcon(R.mipmap.back_icon);//设置左图标 //设置点击事件 setleftbuttonOnClicklinster(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(getContext(),"left",Toast.LENGTH_SHORT).show(); } }); setRightbuttonIcon(R.mipmap.nav_more);//设置右图标 //设置点击事件 setRightbuttonOnClicklinster(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(getContext(),"right",Toast.LENGTH_SHORT).show(); } }); } } private voID initVIEw() { if(vIEw==null){ //初始化 mInflater= LayoutInflater.from(getContext()); //添加布局文件 vIEw=mInflater.inflate(R.layout.toolbar,null); //绑定控件 mEditSearchVIEw= (EditText) vIEw.findVIEwByID(R.ID.toolbar_searchvIEw); mTextTitle= (TextVIEw) vIEw.findVIEwByID(R.ID.toolbar_Title); mleftbutton= (Imagebutton) vIEw.findVIEwByID(R.ID.mleftbutton); mRightbutton= (Imagebutton) vIEw.findVIEwByID(R.ID.mRightbutton); LayoutParams layoutParams = new LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER_HORIZONTAL); addVIEw(vIEw,layoutParams); } } public voID setRightbuttonIcon(int icon){ if(mRightbutton !=null){ mRightbutton.setimageResource(icon); // mRightbutton.setVisibility(VISIBLE); } } public voID setleftbuttonIcon(int icon){ if(mleftbutton !=null){ mleftbutton.setimageResource(icon); //mleftbutton.setVisibility(VISIBLE); } } //设置右侧按钮监听事件 public voID setRightbuttonOnClicklinster(OnClickListener linster) { mRightbutton.setonClickListener(linster); } //设置左侧按钮监听事件 public voID setleftbuttonOnClicklinster(OnClickListener linster) { mleftbutton.setonClickListener(linster); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android自定义Toolbar使用方法详解全部内容,希望文章能够帮你解决Android自定义Toolbar使用方法详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)