1.定義屬性attrs.xml
<!-- MyToolBarView -->
<declare-styleable name="MyToolBarView">
<!-- 是否使用沉浸式 默認false-->
<attr name="useSink" format="boolean" />
<!-- 是否顯示返回鍵 默認true -->
<attr name="showBackIcon" format="boolean" />
<!-- 是否顯示標題 默認true -->
<attr name="showTitleView" format="boolean" />
<!-- 是否顯示功能1圖標 默認false -->
<attr name="showFunIconOne" format="boolean" />
<!-- 是否顯示功能2圖標 默認false -->
<attr name="showFunIconTwo" format="boolean" />
<!-- 是否顯示功能1文字 默認false -->
<attr name="showFunTextOne" format="boolean" />
<!-- 是否顯示功能2文字 默認false -->
<attr name="showFunTextTwo" format="boolean" />
<!-- 設置標題內容 -->
<attr name="title" format="string" />
<!-- 設置功能1文字內容 -->
<attr name="funOneText" format="string" />
<!-- 設置功能2文字內容 -->
<attr name="funTwoText" format="string" />
<!-- 設置返回Icon -->
<attr name="backIcon" format="reference" />
<!-- 設置功能1Icon -->
<attr name="funOneIcon" format="reference" />
<!-- 設置功能2Icon -->
<attr name="funTwoIcon" format="reference" />
</declare-styleable>
2.自定義View
public class MyToolBarView extends FrameLayout implements View.OnClickListener {
//狀態欄的高度
private int statusBarHeight;
//ActionBar的高度
private int actionBarHeight;
//是否使用沉浸式
private boolean useSink;
//是否顯示返回鍵
private boolean showBackIcon;
//是否顯示標題
private boolean showTitleView;
//是否顯示功能1圖標
private boolean showFunIconOne;
//是否顯示功能2圖標
private boolean showFunIconTwo;
//是否顯示功能1文字
private boolean showFunTextOne;
//是否顯示功能2文字
private boolean showFunTextTwo;
//標題內容
private String titleStr;
//功能1文字內容
private String funTextOneStr;
//功能2文字內容
private String funTextTwoStr;
//返回鍵Icon
private Drawable backIconDrawable;
//功能1Icon
private Drawable funOneIconDrawable;
//功能2Icon
private Drawable funTwoIconDrawable;
//標題
private TextView mTitleTv;
//返回Icon
private ImageView mBackBtn;
//功能1Icon
private ImageView mFunOneIconBtn;
//功能2Icon
private ImageView mFunTwoIconBtn;
//功能1文字
private TextView mFunOneTextBtn;
//功能2文字
private TextView mFunTwoTextBtn;
private Context mContext;
private OnFunClickListener mOnFunClickListener;
public interface OnFunClickListener {
void onClick(View view);
}
public void setOnFunClickListener(OnFunClickListener mOnFunClickListener) {
this.mOnFunClickListener = mOnFunClickListener;
}
public MyToolBarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MyToolBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public MyToolBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, @Nullable AttributeSet attrs) {
mContext = context;
//狀態欄高度 默認18dp
statusBarHeight = DpUtils.dp2px(context, 18.0f);
//ActionBar高度 默認45dp
actionBarHeight = DpUtils.dp2px(context, 45.0f);
try {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}
} catch (Exception e) {
e.printStackTrace();
}
//解析XML的屬性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyToolBarView);
useSink = typedArray.getBoolean(R.styleable.MyToolBarView_useSink, false);
showBackIcon = typedArray.getBoolean(R.styleable.MyToolBarView_showBackIcon, true);
showTitleView = typedArray.getBoolean(R.styleable.MyToolBarView_showTitleView, true);
showFunIconOne = typedArray.getBoolean(R.styleable.MyToolBarView_showFunIconOne, false);
showFunIconTwo = typedArray.getBoolean(R.styleable.MyToolBarView_showFunIconTwo, false);
showFunTextOne = typedArray.getBoolean(R.styleable.MyToolBarView_showFunTextOne, false);
showFunTextTwo = typedArray.getBoolean(R.styleable.MyToolBarView_showFunTextTwo, false);
titleStr = typedArray.getString(R.styleable.MyToolBarView_title);
funTextOneStr = typedArray.getString(R.styleable.MyToolBarView_funOneText);
funTextTwoStr = typedArray.getString(R.styleable.MyToolBarView_funTwoText);
backIconDrawable = typedArray.getDrawable(R.styleable.MyToolBarView_backIcon);
if (backIconDrawable == null) {
backIconDrawable = getResources().getDrawable(R.drawable.rtzs_icon_arrow_back);
}
funOneIconDrawable = typedArray.getDrawable(R.styleable.MyToolBarView_funOneIcon);
funTwoIconDrawable = typedArray.getDrawable(R.styleable.MyToolBarView_funTwoIcon);
typedArray.recycle();
//創建ToolBar視圖
createToolBarView(context);
//如果使用沉浸式
if (useSink) {
setPadding(getPaddingLeft(), statusBarHeight + getPaddingTop(), getPaddingRight(), getPaddingBottom());
}
}
private void createToolBarView(Context context) {
View toolBarView = View.inflate(context, R.layout.retech_include_toolbar, this);
mTitleTv = toolBarView.findViewById(R.id.tv_title);
mBackBtn = toolBarView.findViewById(R.id.iv_back);
mFunOneIconBtn = toolBarView.findViewById(R.id.iv_fun_one);
mFunTwoIconBtn = toolBarView.findViewById(R.id.iv_fun_two);
mFunOneTextBtn = toolBarView.findViewById(R.id.tv_fun_one);
mFunTwoTextBtn = toolBarView.findViewById(R.id.tv_fun_two);
//設置屬性
mTitleTv.setVisibility(showTitleView ? VISIBLE : GONE);
mBackBtn.setVisibility(showBackIcon ? VISIBLE : GONE);
mFunOneIconBtn.setVisibility(showFunIconOne ? VISIBLE : GONE);
mFunTwoIconBtn.setVisibility(showFunIconTwo ? VISIBLE : GONE);
mFunOneTextBtn.setVisibility(showFunTextOne ? VISIBLE : GONE);
mFunTwoTextBtn.setVisibility(showFunTextTwo ? VISIBLE : GONE);
mTitleTv.setText(titleStr == null ? "Unknown" : titleStr);
mFunOneTextBtn.setText(funTextOneStr == null ? "Unknown" : funTextOneStr);
mFunTwoTextBtn.setText(funTextTwoStr == null ? "Unknown" : funTextTwoStr);
mBackBtn.setImageDrawable(backIconDrawable);
mFunOneIconBtn.setImageDrawable(funOneIconDrawable);
mFunTwoIconBtn.setImageDrawable(funTwoIconDrawable);
mTitleTv.setOnClickListener(this);
mBackBtn.setOnClickListener(this);
mFunOneIconBtn.setOnClickListener(this);
mFunTwoIconBtn.setOnClickListener(this);
mFunOneTextBtn.setOnClickListener(this);
mFunTwoTextBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.iv_back) {
((BaseActavity) mContext).finishByAnim();
} else if (id == R.id.tv_title || id == R.id.iv_fun_one || id == R.id.iv_fun_two
|| id == R.id.tv_fun_one || id == R.id.tv_fun_two) {
if (mOnFunClickListener != null) {
mOnFunClickListener.onClick(v);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int measureHeight(int heightMeasureSpec) {
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int specSize = MeasureSpec.getSize(heightMeasureSpec);
int result;
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = getPaddingBottom() + getPaddingTop() + actionBarHeight;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
/**
* 設置ToolBar Title
*
* @param title
*/
public void setToolBarTitle(String title) {
if (mTitleTv != null) {
mTitleTv.setText(title);
}
}
/**
* 設置ToolBar Title
*
* @param resid
*/
public void setToolBarTitle(int resid) {
if (mTitleTv != null) {
mTitleTv.setText(getResources().getString(resid));
}
}
}
3.測量ActionBar高度方法
try {
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}
} catch (Exception e) {
e.printStackTrace();
}
4.測量狀態欄高度的方法
try {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
} catch (Exception e) {
e.printStackTrace();
}
5.XML佈局
<?xml version="1.0" encoding="utf-8"?><!-- ToolBar -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
tools:background="@color/retech_appyes">
<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:scaleType="centerInside"
android:src="@drawable/rtzs_icon_arrow_back" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="18sp"
tools:text="標題" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_fun_one"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:scaleType="centerInside"
tools:src="@drawable/rtzs_icon_shezhi" />
<ImageView
android:id="@+id/iv_fun_two"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:scaleType="centerInside"
android:visibility="gone"
tools:src="@drawable/rtzs_icon_shezhi" />
<TextView
android:id="@+id/tv_fun_one"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textColor="@android:color/white"
android:textSize="17sp"
android:visibility="gone"
tools:text="功能1" />
<TextView
android:id="@+id/tv_fun_two"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textColor="@android:color/white"
android:textSize="17sp"
android:visibility="gone"
tools:text="功能2" />
</LinearLayout>
</RelativeLayout>