国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義Toolbar使用方法詳解

2019-12-12 04:11:49
字體:
來源:轉載
供稿:網友

本篇文章介紹:

如何使用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, attrs, 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);  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上高县| 扶沟县| 湾仔区| 石台县| 治县。| 酉阳| 通山县| 宜章县| 温州市| 普兰店市| 集贤县| 海兴县| 岳阳市| 中西区| 和林格尔县| 娄烦县| 宁津县| 海口市| 榕江县| 蕲春县| 福清市| 边坝县| 垦利县| 通州市| 尚义县| 马龙县| 绥化市| 名山县| 衡水市| 龙口市| 姜堰市| 武宁县| 河池市| 特克斯县| 甘肃省| 资源县| 文水县| 伊宁市| 集贤县| 杭州市| 明光市|