Android自定義Toolbar使用方法詳解
本篇文章介紹:
如何使用Toolbar;
自定義Toolbar;
先來(lái)看一看效果,了解一下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中設(shè)置屬性:
Toolbar toolBar= (Toolbar) findViewById(R.id.toolbar);
toolBar.setLogo(R.mipmap.ic_launcher);//設(shè)置圖標(biāo)
toolBar.setTitle("Title");//設(shè)置主標(biāo)題
toolBar.setSubtitle("smalltitle");//設(shè)置子標(biāo)題
這樣就可以實(shí)現(xiàn)上面的效果。
接下來(lái)是自定義的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>
新建一個(gè)MyToolbar:
public class MyToolBar extends Toolbar {
//布局
private LayoutInflater mInflater;
//右邊按鈕
private ImageButton mRightButton;
//左邊按鈕
private ImageButton mLeftButton;
//標(biāo)題
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, attrs, defStyleAttr);
//初始化函數(shù)
initView();
setContentInsetsRelative(10, 10);
if (attrs != null) {
setLeftButtonIcon(R.mipmap.back_icon);//設(shè)置左圖標(biāo)
//設(shè)置點(diǎn)擊事件
setLeftButtonOnClickLinster(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"left",Toast.LENGTH_SHORT).show();
}
});
setRightButtonIcon(R.mipmap.nav_more);//設(shè)置右圖標(biāo)
//設(shè)置點(diǎn)擊事件
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);
}
}
//設(shè)置右側(cè)按鈕監(jiān)聽事件
public void setRightButtonOnClickLinster(OnClickListener linster) {
mRightButton.setOnClickListener(linster);
}
//設(shè)置左側(cè)按鈕監(jiān)聽事件
public void setLeftButtonOnClickLinster(OnClickListener linster) {
mLeftButton.setOnClickListener(linster);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼
- Android頂部(toolbar)搜索框?qū)崿F(xiàn)的實(shí)例詳解
- Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼
- Android ToolBar 修改邊距的實(shí)現(xiàn)方法
- Android ToolBar控件詳解及實(shí)例
- Android動(dòng)態(tài)修改ToolBar的Menu菜單示例
- Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
- Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
相關(guān)文章
Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能示例
這篇文章主要介紹了Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能,結(jié)合具體實(shí)例形式分析了Android基于HttpURLConnection的客戶端結(jié)合Go語(yǔ)言服務(wù)器端實(shí)現(xiàn)文件上傳功能的操作技巧,需要的朋友可以參考下2017-03-03
如何使用Android實(shí)現(xiàn)接口實(shí)信息在留言板顯示
這篇文章主要介紹了如何使用Android接口實(shí)現(xiàn)信息的留言板顯示,需要的朋友可以參考下2015-07-07
Android編程實(shí)現(xiàn)添加低電流提醒功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)添加低電流提醒功能的方法,涉及Android廣播監(jiān)聽及電源監(jiān)控等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android本地存儲(chǔ)SharedPreferences詳解
這篇文章主要介紹了Android本地存儲(chǔ)SharedPreferences詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android自定義控件eBook實(shí)現(xiàn)翻書效果實(shí)例詳解
這篇文章主要介紹了Android自定義控件eBook實(shí)現(xiàn)翻書效果的方法,結(jié)合實(shí)例形式分析了Android自定義控件實(shí)現(xiàn)翻書效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10

