1.首先定义你要的toolbar的布局:toolbar.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/toolbar_searchview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_centerVertical="true" android:gravity="center" android:drawableLeft="@mipmap/icon_search" style="@style/search_view" android:hint="请输入搜索内容" android:visibility="gone" /> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:gravity="center" android:textColor="@color/white" android:textSize="20sp" android:visibility="gone" /> <Button android:id="@+id/toolbar_rightButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:textColor="@color/white" android:visibility="gone" style="@android:style/Widget.Material.Toolbar.Button.Navigation" /> </RelativeLayout>
2.在values中新建一个attrs.xml文件,然后在这个文件中新建引用代码:
<declare-styleable name="CNiaoToolBar"> <attr name="rightButtonIcon1" format="reference"/> <attr name="isShowSearchView1" format="boolean"/> <attr name="rightButtonText1" format="string"/> </declare-styleable> </resources> <!--format="refreence"这个是来规定取值范围--> <!--refrence这个是为了可以调用自定义的button的图片--> <!--boolran这个是为
3.编写自定义的class类CNiaoToolBar:
package zuo.com.ui.widget; import android.annotation.TargetApi; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v7.internal.widget.TintTypedArray; import android.support.v7.widget.Toolbar; import android.util.AttributeSet; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import zuo.com.ui.R; public class CNiaoToolBar extends Toolbar { private LayoutInflater mInflater; private View mView; private TextView mTextTitle; private EditText mSearchView; private Button mRightButton; public CNiaoToolBar(Context context) { this(context,null); } public CNiaoToolBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CNiaoToolBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); setContentInsetsRelative(10,10); if(attrs !=null) { final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, R.styleable.CNiaoToolBar, defStyleAttr, 0); final Drawable rightIcon = a.getDrawable(R.styleable.CNiaoToolBar_rightButtonIcon1); if (rightIcon != null) { //setNavigationIcon(navIcon); setRightButtonIcon(rightIcon); } boolean isShowSearchView = a.getBoolean(R.styleable.CNiaoToolBar_isShowSearchView1,false); if(isShowSearchView){ showSearchView(); hideTitleView(); } CharSequence rightButtonText = a.getText(R.styleable.CNiaoToolBar_rightButtonText1); if(rightButtonText !=null){ setRightButtonText(rightButtonText); } a.recycle(); } } private void initView() { if(mView == null) { mInflater = LayoutInflater.from(getContext()); mView = mInflater.inflate(R.layout.toolbar, null); mTextTitle = (TextView) mView.findViewById(R.id.toolbar_title); mSearchView = (EditText) mView.findViewById(R.id.toolbar_searchview); mRightButton = (Button) mView.findViewById(R.id.toolbar_rightButton); LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL); addView(mView, lp); } } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public void setRightButtonIcon(Drawable icon){ if(mRightButton !=null){ mRightButton.setBackground(icon); mRightButton.setVisibility(VISIBLE); } } public void setRightButtonIcon(int icon){ setRightButtonIcon(getResources().getDrawable(icon)); } public void setRightButtonOnClickListener(OnClickListener li){ mRightButton.setOnClickListener(li); } public void setRightButtonText(CharSequence text){ mRightButton.setText(text); mRightButton.setVisibility(VISIBLE); } public void setRightButtonText(int id){ setRightButtonText(getResources().getString(id)); } public Button getRightButton(){ return this.mRightButton; } @Override public void setTitle(int resId) { setTitle(getContext().getText(resId)); } @Override public void setTitle(CharSequence title) { initView(); if(mTextTitle !=null) { mTextTitle.setText(title); showTitleView(); } } public void showSearchView(){ if(mSearchView !=null) mSearchView.setVisibility(VISIBLE); } public void hideSearchView(){ if(mSearchView !=null) mSearchView.setVisibility(GONE); } public void showTitleView(){ if(mTextTitle !=null) mTextTitle.setVisibility(VISIBLE); } public void hideTitleView() { if (mTextTitle != null) mTextTitle.setVisibility(GONE); } // // private void ensureRightButtonView() { // if (mRightImageButton == null) { // mRightImageButton = new ImageButton(getContext(), null, // android.support.v7.appcompat.R.attr.toolbarNavigationButtonStyle); // final LayoutParams lp = generateDefaultLayoutParams(); // lp.gravity = GravityCompat.START | (Gravity.VERTICAL_GRAVITY_MASK); // mRightImageButton.setLayoutParams(lp); // } // } }
4.之后就是在activity布局中加载<CNiaoToolBar/>
<zuo.com.ui.widget.CNiaoToolBar android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:isShowSearchView="false" app:navigationIcon="@mipmap/icon_back_32px" app:contentInsetEnd="56dp" app:title="商品信息"/>
在Acticity中引用CNiaoToolBar:
package zuo.com.ui; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import zuo.com.ui.widget.ByToolBar; import zuo.com.ui.widget.CNiaoToolBar; /** * Created by Administrator on 2016/10/21. */ public class ShopMsgActivity extends AppCompatActivity implements View.OnClickListener { private CNiaoToolBar byToolBar; private int a=0; @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_shop_msg_activity); byToolBar= (CNiaoToolBar) findViewById(R.id.tool_bar); initToolBar(); } private void initToolBar(){ byToolBar.setNavigationOnClickListener(this); byToolBar.getRightButton().setBackgroundColor(getResources().getColor(R.color.red)); byToolBar.setRightButtonText("分享"); byToolBar.setRightButtonOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(a==0){ byToolBar.getRightButton().setTextColor(getResources().getColor(R.color.material_yellow)); a=1; }else { byToolBar.getRightButton().setTextColor(getResources().getColor(R.color.white)); a=0; } } }); } @Override public void onClick(View v) { this.finish(); } }
效果图: