虽然android提供了toolbar供我们使用,但是经常不能满足我们的需求,所以通常需要自定义统一的actionBar
1.编写actionBar的布局
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp" xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView android:id="@+id/tv_left_actionbar_title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:text="更多" /> <ImageView android:id="@+id/iv_left_actionbar_title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow_left_back"/> <TextView android:id="@+id/tv_center_actionbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:text="标题" /> <TextView android:id="@+id/tv_right_actionbar_title_more" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" android:text="更多" /> <ImageView android:id="@+id/iv_right_actionbar_title_more" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" android:text="标题" android:src="@drawable/arrow_left_back" /> </android.support.constraint.ConstraintLayout> 2.编写自定义控件所需要的属性(包括标题 左右资源等) <declare-styleable name="CustomActionBar"> <attr name="title" format="string"/> <attr name="left_icon" format="reference"/> <attr name="right_icon" format="reference"/> <attr name="left_text" format="string"/> <attr name="right_text" format="string"/> <attr name="custom_actionbar_type" format="integer"/> </declare-styleable> 3.自定义ActionBar并设置相关属性和监听
package com.dejun.commonsdk.wight; import android.content.Context; import android.content.res.TypedArray; import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.dejun.commonsdk.R; /** * Author:DoctorWei * Time:2018/12/18 10:20 * Description:android自定义标题栏 * email:1348172474@qq.com */ public class CustomActionBar extends RelativeLayout implements View.OnClickListener{ private TextView mTvTitle; private ImageView mIvLeftBack; private TextView mTvLeftBack; private TextView mTvRightMore; private ImageView mIvRightMore; public CustomActionBar(Context context, AttributeSet attrs) { super(context, attrs); setId(R.id.custom_action_bar); initViews(context,attrs); } private void initViews(Context context,AttributeSet attrs) { View view= LayoutInflater.from(context).inflate(R.layout.custom_actionbar_layout,this);//需要将布局放到自定义view中否则不显示 mTvTitle=view.findViewById(R.id.tv_center_actionbar_title);//中间标题 mIvLeftBack=view.findViewById(R.id.iv_left_actionbar_title_back);//图标返回 mTvLeftBack=view.findViewById(R.id.tv_left_actionbar_title_back);//文字返回 mIvRightMore=view.findViewById(R.id.iv_right_actionbar_title_more);//图标更多 mTvRightMore=view.findViewById(R.id.tv_right_actionbar_title_more);//文字更多 //获取布局中设置的属性 initAttrData(context,attrs); } private void initAttrData(Context context,AttributeSet attrs) { TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomActionBar); String title = typedArray.getString(R.styleable.CustomActionBar_title); int leftIcon= typedArray.getResourceId(R.styleable.CustomActionBar_left_icon,R.drawable.arrow_left_back); int rightIcon = typedArray.getResourceId(R.styleable.CustomActionBar_right_icon,R.drawable.arrow_left_back); String leftText = typedArray.getString(R.styleable.CustomActionBar_left_text); String rightText = typedArray.getString(R.styleable.CustomActionBar_right_text); if (!TextUtils.isEmpty(title)) { mTvTitle.setVisibility(View.VISIBLE); mTvTitle.setText(title); }else{ mTvTitle.setVisibility(View.GONE); } if (!TextUtils.isEmpty(leftText)) { mTvLeftBack.setVisibility(View.VISIBLE); mTvLeftBack.setText(leftText); mIvLeftBack.setVisibility(View.GONE); }else{ mTvLeftBack.setVisibility(View.GONE); mIvLeftBack.setVisibility(View.VISIBLE); mIvLeftBack.setImageResource(leftIcon); } if (!TextUtils.isEmpty(rightText)) { mTvRightMore.setVisibility(View.VISIBLE); mTvRightMore.setText(rightText); mIvRightMore.setVisibility(View.GONE); }else{ mTvRightMore.setVisibility(View.GONE); mIvRightMore.setVisibility(View.VISIBLE); mIvRightMore.setImageResource(rightIcon); } mIvLeftBack.setOnClickListener(this); mTvLeftBack.setOnClickListener(this); mIvRightMore.setOnClickListener(this); mTvRightMore.setOnClickListener(this); } @Override public void onClick(View v) { int id=v.getId(); if (id == R.id.iv_left_actionbar_title_back) { actionBarListener.ivLeftBackClick(id); }else if (id == R.id.tv_left_actionbar_title_back){ actionBarListener.tvLeftBackClick(id); }else if (id == R.id.iv_right_actionbar_title_more){ actionBarListener.ivRightBackClick(id); }else if (id == R.id.tv_right_actionbar_title_more){ actionBarListener.tvRightLeftBackClick(id); } } public CustomActionBar setActionBarListener(ActionBarListener actionBarListener){ this.actionBarListener=actionBarListener; return this; } public ActionBarListener actionBarListener; public abstract static class ActionBarListener{ public void ivLeftBackClick(int id){ } public void tvLeftBackClick(int id){ } public void ivRightBackClick(int id){ } public void tvRightLeftBackClick(int id){ } } } 4.使用 <com.dejun.commonsdk.wight.CustomActionBar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff1" app:left_icon="@drawable/arrow_left_back" app:title="标题" app:right_icon="@drawable/arrow_left_back"> </com.dejun.commonsdk.wight.CustomActionBar>