注意:標(biāo)題欄已經(jīng)適配沉浸式狀態(tài)欄
廢話不多說直接上代碼:
package driver.daijia.com.driver.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import driver.daijia.com.driver.R;
import driver.daijia.com.driver.frame.utils.DensityUtil;
import driver.daijia.com.driver.frame.utils.TextUtil;
import static android.view.Gravity.CENTER;
/**
* .----.
* _.'__? ? `.
* .--(Q)(OK)---/$\
* .' @? ? ? ? ? /$$$\
* :? ? ? ? ,? $$$$$
* `-..__.-' _.-\$$/
* `;_:? ? `"'
* .'"""""`.
* /,? FLY? ,\
* //? ? ? ? \\
* `-._______.-'
* ___`. | .'___
* (______|______)
* SongWenjun
* Created by dell
* on 2020/6/17
* The package is driver.daijia.com.driver.view
* This Class is ...? 自定義沉浸式狀態(tài)欄
*/
public class TitleBarView extends RelativeLayout {
? ? //---------------------------------------------定義id-------------------------------------------
? ? @SuppressLint("ResourceType")
? ? @android.support.annotation.IdRes
? ? int RL_TITLEBAR = 100001;
? ? @SuppressLint("ResourceType")
? ? @android.support.annotation.IdRes
? ? int TV_TITLE = 100002;
? ? @SuppressLint("ResourceType")
? ? @android.support.annotation.IdRes
? ? int IMG_FINIGH = 100003;
? ? @SuppressLint("ResourceType")
? ? @android.support.annotation.IdRes
? ? int IMG_MOVE = 100004;
? ? @SuppressLint("ResourceType")
? ? @android.support.annotation.IdRes
? ? int TV_DESCRIBE = 100005;
? ? RelativeLayout rlTitleBar;
? ? private TextView tvTitle;
? ? private ImageView imgFinish;
? ? private TextView tvDescribe;
? ? private ImageView imgMove;
? ? private boolean isMmersion;//是否使用沉浸式狀態(tài)欄
? ? private String describe;//描述
? ? private String title;//標(biāo)題
? ? private Drawable imgFinishsrc;//返回的圖片值
? ? private Drawable imgMovesrc;//更多的圖片值
? ? private int style;//風(fēng)格
? ? //--------------------------------------------定義風(fēng)格------------------------------------------
? ? public final static int STYLE_FINISH_TITLE = 0;
? ? public final static int STYLE_FINISH_TITLE_TEXT = 1;
? ? public final static int STYLE_FINISH_TITLE_MOVE = 2;
? ? public final static int STYLE_FINISH_TITLE_TEXT_MOVE = 3;
? ? public final static int STYLE_FINISH_TITLE_MOVE_TEXT = 4;
? ? public final static int STYLE_TITLE = 5;
? ? //------------------------------------------設(shè)置事件--------------------------------------------
? ? private OnFinishClick onFinishClick;//返回鍵的點(diǎn)擊事件
? ? private OnMoveClick onMoveClick;//更多的點(diǎn)擊事件
? ? private OnDescribeClick onDescribeClick;//描述的點(diǎn)擊事件
? ? public void setOnFinishClick(OnFinishClick onFinishClick) {
? ? ? ? this.onFinishClick = onFinishClick;
? ? }
? ? public void setOnMoveClick(OnMoveClick onMoveClick) {
? ? ? ? this.onMoveClick = onMoveClick;
? ? }
? ? public void setOnDescribeClick(OnDescribeClick onDescribeClick) {
? ? ? ? this.onDescribeClick = onDescribeClick;
? ? }
? ? public TextView getTvTitle() {
? ? ? ? return tvTitle;
? ? }
? ? public void setTvTitle(TextView tvTitle) {
? ? ? ? this.tvTitle = tvTitle;
? ? }
? ? public ImageView getImgFinish() {
? ? ? ? return imgFinish;
? ? }
? ? public void setImgFinish(ImageView imgFinish) {
? ? ? ? this.imgFinish = imgFinish;
? ? }
? ? public TextView getTvDescribe() {
? ? ? ? return tvDescribe;
? ? }
? ? public void setTvDescribe(TextView tvDescribe) {
? ? ? ? this.tvDescribe = tvDescribe;
? ? }
? ? public ImageView getImgMove() {
? ? ? ? return imgMove;
? ? }
? ? public void setImgMove(ImageView imgMove) {
? ? ? ? this.imgMove = imgMove;
? ? }
? ? public boolean isMmersion() {
? ? ? ? return isMmersion;
? ? }
? ? public void setMmersion(boolean mmersion) {
? ? ? ? isMmersion = mmersion;
? ? }
? ? public String getDescribe() {
? ? ? ? return describe;
? ? }
? ? public void setDescribe(String describe) {
? ? ? ? this.describe = describe;
? ? }
? ? public String getTitle() {
? ? ? ? return title;
? ? }
? ? public void setTitle(String title) {
? ? ? ? this.title = title;
? ? }
? ? public Drawable getImgFinishsrc() {
? ? ? ? return imgFinishsrc;
? ? }
? ? public void setImgFinishsrc(Drawable imgFinishsrc) {
? ? ? ? this.imgFinishsrc = imgFinishsrc;
? ? }
? ? public Drawable getImgMovesrc() {
? ? ? ? return imgMovesrc;
? ? }
? ? public void setImgMovesrc(Drawable imgMovesrc) {
? ? ? ? this.imgMovesrc = imgMovesrc;
? ? }
? ? public int getStyle() {
? ? ? ? return style;
? ? }
? ? public TitleBarView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? initView(context);
? ? ? ? initAttributeSet(context, attrs);
? ? }
? ? public TitleBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? initView(context);
? ? ? ? initAttributeSet(context, attrs);
? ? }
? ? /**
? ? * 初始化自定義屬性
? ? *
? ? * @param attrs
? ? */
? ? private void initAttributeSet(Context context, @Nullable AttributeSet attrs) {
? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);
? ? ? ? if (typedArray != null) {
? ? ? ? ? ? isMmersion = typedArray.getBoolean(R.styleable.TitleBarView_is_immersion, true);
? ? ? ? ? ? describe = typedArray.getString(R.styleable.TitleBarView_describe);
? ? ? ? ? ? title = typedArray.getString(R.styleable.TitleBarView_title);
? ? ? ? ? ? imgFinishsrc = typedArray.getDrawable(R.styleable.TitleBarView_img_finish_src);
? ? ? ? ? ? imgMovesrc = typedArray.getDrawable(R.styleable.TitleBarView_img_move_src);
? ? ? ? ? ? style = typedArray.getInteger(R.styleable.TitleBarView_titlebar_style, -1);
? ? ? ? ? ? if (style == -1) {
? ? ? ? ? ? ? ? style = STYLE_FINISH_TITLE_TEXT;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //設(shè)置屬性數(shù)據(jù)
? ? ? ? if (TextUtil.isEmpty(title)) {
? ? ? ? ? ? tvTitle.setText("我是標(biāo)題");
? ? ? ? } else {
? ? ? ? ? ? tvTitle.setText(title);
? ? ? ? }
? ? ? ? if (TextUtil.isEmpty(describe)) {
? ? ? ? ? ? tvDescribe.setText("描述");
? ? ? ? } else {
? ? ? ? ? ? tvDescribe.setText(describe);
? ? ? ? }
? ? ? ? if (null == imgFinishsrc) {
? ? ? ? ? ? imgFinish.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.img_finish_white));
? ? ? ? } else {
? ? ? ? ? ? imgFinish.setImageDrawable(imgFinishsrc);
? ? ? ? }
? ? ? ? if (null == imgMovesrc) {
? ? ? ? ? ? imgMove.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.img_finish_white));
? ? ? ? } else {
? ? ? ? ? ? imgMove.setImageDrawable(imgMovesrc);
? ? ? ? }
? ? ? ? setStyle(style);
? ? }
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? if (isMmersion)
? ? ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec + getStatusBarHeight(getContext()));
? ? ? ? else
? ? ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? measureChildren(widthMeasureSpec, heightMeasureSpec);
? ? ? ? int widthMode = MeasureSpec.getMode(widthMeasureSpec);
? ? ? ? int width = MeasureSpec.getSize(widthMeasureSpec);
? ? ? ? int heightMode = MeasureSpec.getMode(heightMeasureSpec);
? ? ? ? int height = MeasureSpec.getSize(heightMeasureSpec);
? ? ? ? if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
? ? ? ? ? ? int groupWidth = getMaxWidth();
? ? ? ? ? ? int groupHeight = getTotalHeight();
? ? ? ? ? ? setMeasuredDimension(groupWidth, groupHeight);
? ? ? ? } else if (widthMode == MeasureSpec.AT_MOST) {
? ? ? ? ? ? setMeasuredDimension(getMaxWidth(), height);
? ? ? ? } else if (heightMode == MeasureSpec.AT_MOST) {
? ? ? ? ? ? setMeasuredDimension(width, getTotalHeight());
? ? ? ? }
? ? }
? ? private int getMaxWidth() {
? ? ? ? int count = getChildCount();
? ? ? ? int maxWidth = 0;
? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? int currentWidth = getChildAt(i).getMeasuredWidth();
? ? ? ? ? ? if (maxWidth < currentWidth) {
? ? ? ? ? ? ? ? maxWidth = currentWidth;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return maxWidth;
? ? }
? ? private int getTotalHeight() {
? ? ? ? int count = getChildCount();
? ? ? ? int totalHeight = 0;
? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? totalHeight += getChildAt(i).getMeasuredHeight();
? ? ? ? }
? ? ? ? return totalHeight;
? ? }
? ? @Override
? ? protected void onLayout(boolean changed, int l, int t, int r, int b) {
? ? ? ? int count = getChildCount();
? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? View view = getChildAt(i);
? ? ? ? ? ? if (isMmersion) {
? ? ? ? ? ? ? ? view.layout(l, getStatusBarHeight(getContext()), r, b);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? view.layout(l, t, r, b);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? private void initView(Context context) {
? ? ? ? int dp15 = DensityUtil.dip2px(context, 15);
? ? ? ? rlTitleBar = new RelativeLayout(context);
? ? ? ? RelativeLayout.LayoutParams rlTitleBarParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
? ? ? ? rlTitleBarParams.addRule(ALIGN_PARENT_BOTTOM);
? ? ? ? rlTitleBar.setLayoutParams(rlTitleBarParams);
? ? ? ? rlTitleBar.setId(RL_TITLEBAR);
? ? ? ? tvTitle = new TextView(context);
? ? ? ? tvTitle.setId(TV_TITLE);
? ? ? ? imgFinish = new ImageView(context);
? ? ? ? imgFinish.setId(IMG_FINIGH);
? ? ? ? tvDescribe = new TextView(context);
? ? ? ? tvDescribe.setId(TV_DESCRIBE);
? ? ? ? imgMove = new ImageView(context);
? ? ? ? imgMove.setId(IMG_MOVE);
? ? ? ? //添加布局
? ? ? ? rlTitleBar.addView(tvTitle);
? ? ? ? rlTitleBar.addView(imgFinish);
? ? ? ? rlTitleBar.addView(tvDescribe);
? ? ? ? rlTitleBar.addView(imgMove);
? ? ? ? //-------------------------------------------設(shè)置標(biāo)題文字信息-----------------------------------------
? ? ? ? LayoutParams tvTitlePramas = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
? ? ? ? tvTitle.setGravity(CENTER);
? ? ? ? tvTitle.setLayoutParams(tvTitlePramas);
? ? ? ? tvTitle.setText("我是標(biāo)題");
? ? ? ? setTextColor(tvTitle, R.color.white);
? ? ? ? setTextSize(tvTitle, 20);
? ? ? ? //-------------------------------------------設(shè)置返回按鈕位置等信息-----------------------------------------
? ? ? ? imgFinish.setScaleType(ImageView.ScaleType.CENTER);//設(shè)置圖片為居中位置
? ? ? ? imgFinish.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.img_finish_white));
? ? ? ? int dp45 = DensityUtil.dip2px(context, 45);
? ? ? ? RelativeLayout.LayoutParams imgFinishLayoutParams = new RelativeLayout.LayoutParams(dp45, LayoutParams.MATCH_PARENT);
? ? ? ? imgFinish.setLayoutParams(imgFinishLayoutParams);
? ? ? ? //-----------------------------------------設(shè)置右側(cè)文字內(nèi)容------------------------------------------------
? ? ? ? RelativeLayout.LayoutParams tvDescribeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
? ? ? ? tvDescribeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//設(shè)置位于最右邊
? ? ? ? tvDescribeParams.addRule(RelativeLayout.CENTER_VERTICAL);//設(shè)置位于最右邊
? ? ? ? tvDescribeParams.setMargins(0, 0, dp15, 0);//設(shè)置右外邊距
? ? ? ? tvDescribe.setLayoutParams(tvDescribeParams);
? ? ? ? tvDescribe.setText("描述");
? ? ? ? setTextColor(tvDescribe, R.color.white);
? ? ? ? setTextSize(tvDescribe, 14);
? ? ? ? //----------------------------------------設(shè)置右側(cè)圖片位置--------------------------------------------
? ? ? ? RelativeLayout.LayoutParams imgMoveParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
? ? ? ? imgMoveParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//設(shè)置位于最右邊
? ? ? ? imgMoveParams.addRule(RelativeLayout.CENTER_VERTICAL);//設(shè)置位于最右邊
? ? ? ? imgMoveParams.setMargins(0, 0, dp15, 0);//設(shè)置右外邊距
? ? ? ? imgMove.setScaleType(ImageView.ScaleType.CENTER);
? ? ? ? imgMove.setLayoutParams(imgMoveParams);
? ? ? ? //設(shè)置整體
? ? ? ? addView(rlTitleBar);
? ? ? ? initViewClick();
? ? }
? ? private void initViewClick() {
? ? ? ? imgFinish.setOnClickListener(v -> {
? ? ? ? ? ? if (onFinishClick != null)
? ? ? ? ? ? ? ? onFinishClick.onFinishClick(v);
? ? ? ? });
? ? ? ? imgMove.setOnClickListener(v -> {
? ? ? ? ? ? if (onMoveClick != null)
? ? ? ? ? ? ? ? onMoveClick.onMoveClick(v);
? ? ? ? });
? ? ? ? tvDescribe.setOnClickListener(v -> {
? ? ? ? ? ? if (onDescribeClick != null)
? ? ? ? ? ? ? ? onDescribeClick.onDescribeClick(v);
? ? ? ? });
? ? }
? ? /**
? ? * 設(shè)置標(biāo)題欄風(fēng)格
? ? *
? ? * @param style
? ? */
? ? public void setStyle(int style) {
? ? ? ? switch (style) {
? ? ? ? ? ? case STYLE_FINISH_TITLE_TEXT_MOVE:
? ? ? ? ? ? case STYLE_FINISH_TITLE_MOVE_TEXT:
? ? ? ? ? ? ? ? imgMove.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? imgFinish.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? tvDescribe.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? tvTitle.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case STYLE_FINISH_TITLE_MOVE:
? ? ? ? ? ? ? ? imgMove.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? imgFinish.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? tvDescribe.setVisibility(GONE);
? ? ? ? ? ? ? ? tvTitle.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case STYLE_FINISH_TITLE_TEXT:
? ? ? ? ? ? ? ? imgMove.setVisibility(GONE);
? ? ? ? ? ? ? ? imgFinish.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? tvDescribe.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? tvTitle.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case STYLE_TITLE:
? ? ? ? ? ? ? ? imgMove.setVisibility(GONE);
? ? ? ? ? ? ? ? imgFinish.setVisibility(GONE);
? ? ? ? ? ? ? ? tvDescribe.setVisibility(GONE);
? ? ? ? ? ? ? ? tvTitle.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case STYLE_FINISH_TITLE:
? ? ? ? ? ? ? ? imgMove.setVisibility(GONE);
? ? ? ? ? ? ? ? imgFinish.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? tvDescribe.setVisibility(GONE);
? ? ? ? ? ? ? ? tvTitle.setVisibility(VISIBLE);
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? int dp15 = DensityUtil.dip2px(getContext(), 15);
? ? ? ? switch (style) {
? ? ? ? ? ? case STYLE_FINISH_TITLE_MOVE_TEXT: {
? ? ? ? ? ? ? ? RelativeLayout.LayoutParams imgMoveParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
? ? ? ? ? ? ? ? imgMoveParams.addRule(RelativeLayout.CENTER_VERTICAL);
? ? ? ? ? ? ? ? imgMoveParams.addRule(RelativeLayout.LEFT_OF, tvDescribe.getId());
? ? ? ? ? ? ? ? imgMoveParams.setMargins(0, 0, dp15, 0);//設(shè)置右外邊距
? ? ? ? ? ? ? ? imgMove.setLayoutParams(imgMoveParams);
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? ? ? case STYLE_FINISH_TITLE_TEXT_MOVE: {
? ? ? ? ? ? ? ? RelativeLayout.LayoutParams tvDescribeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
? ? ? ? ? ? ? ? tvDescribeParams.addRule(RelativeLayout.LEFT_OF, imgMove.getId());//設(shè)置位于最右邊
? ? ? ? ? ? ? ? tvDescribeParams.addRule(RelativeLayout.CENTER_VERTICAL);//設(shè)置位于最右邊
? ? ? ? ? ? ? ? tvDescribeParams.setMargins(0, 0, dp15, 0);//設(shè)置右外邊距
? ? ? ? ? ? ? ? tvDescribe.setLayoutParams(tvDescribeParams);
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? /**
? ? * 獲取狀態(tài)欄高度
? ? *
? ? * @param context
? ? * @return
? ? */
? ? private static int getStatusBarHeight(Context context) {
? ? ? ? Resources resources = context.getResources();
? ? ? ? int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
? ? ? ? int height = resources.getDimensionPixelSize(resourceId);
? ? ? ? return height;
? ? }
? ? public void setTextColor(TextView textView, @ColorRes int color) {
? ? ? ? textView.setTextColor(getResources().getColor(color));
? ? }
? ? public void setTextSize(TextView textView, float size) {
? ? ? ? textView.setTextSize(size);
? ? }
? ? /**
? ? * 設(shè)置標(biāo)題
? ? *
? ? * @param title
? ? */
? ? public void setTitleText(String title) {
? ? ? ? tvTitle.setText(title);
? ? }
? ? public String getTitleText() {
? ? ? ? return tvTitle.getText().toString();
? ? }
? ? /**
? ? * 設(shè)置描述
? ? *
? ? * @param describe
? ? */
? ? public void setDescribeText(String describe) {
? ? ? ? tvDescribe.setText(describe);
? ? }
? ? public String getDescribeText() {
? ? ? ? return tvDescribe.getText().toString();
? ? }
? ? public interface OnFinishClick {
? ? ? ? void onFinishClick(View img);
? ? }
? ? public interface OnMoveClick {
? ? ? ? void onMoveClick(View img);
? ? }
? ? public interface OnDescribeClick {
? ? ? ? void onDescribeClick(View view);
? ? }
}
以上就是標(biāo)題欄的全部代碼敢朱,當(dāng)然也有自定義屬性如下:
<declare-styleable name="TitleBarView">
? ? <attr name="is_immersion" format="boolean" />
? ? <attr name="title" format="string" />
? ? <attr name="describe" format="string" />
? ? <attr name="img_move_src" format="reference" />
? ? <attr name="img_finish_src" format="reference" />
? ? <attr name="titlebar_style" format="integer">
? ? ? ? <enum name="finish_title" value="0" />
? ? ? ? <enum name="finish_title_text" value="1" />
? ? ? ? <enum name="finish_title_move" value="2" />
? ? ? ? <enum name="finish_title_text_move" value="3" />
? ? ? ? <enum name="finish_title_move_text" value="4" />
? ? ? ? <enum name="title" value="5" />
? ? </attr>
</declare-styleable>
當(dāng)然是用起來也是相當(dāng)簡(jiǎn)單的:
<driver.daijia.com.driver.view.TitleBarView
? ? android:layout_width="match_parent"
? ? android:layout_height="?attr/actionBarSize"
? ? app:img_finish_src="@mipmap/img_finish_white"
? ? app:img_move_src="@mipmap/img_finish_white"
? ? app:title="注冊(cè)"
? ? app:titlebar_style="finish_title_move_text" />
還有很多功能不一一演示了。
分享出來,希望能幫助很多同事弛作,減少一部分工期時(shí)間烫罩,如果有漏洞或是性能問題共苛,請(qǐng)與我聯(lián)系轨域,謝謝侠坎。