欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義Toolbar使用方法詳解

 更新時(shí)間:2016年12月27日 17:16:17   作者:java是最好的語言  
這篇文章主要為大家詳細(xì)介紹了Android自定義Toolbar使用方法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本篇文章介紹:

如何使用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中設(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)上面的效果。

接下來是自定義的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);
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論