注意:标题栏已经适配沉浸式状态栏
废话不多说直接上代码:
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 ... 自定义沉浸式状态栏
*/
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;//是否使用沉浸式状态栏
private String describe;//描述
private String title;//标题
private Drawable imgFinishsrc;//返回的图片值
private Drawable imgMovesrc;//更多的图片值
private int style;//风格
//--------------------------------------------定义风格------------------------------------------
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;
//------------------------------------------设置事件--------------------------------------------
private OnFinishClick onFinishClick;//返回键的点击事件
private OnMoveClick onMoveClick;//更多的点击事件
private OnDescribeClick onDescribeClick;//描述的点击事件
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;
}
}
//设置属性数据
if (TextUtil.isEmpty(title)) {
tvTitle.setText("我是标题");
} 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);
//-------------------------------------------设置标题文字信息-----------------------------------------
LayoutParams tvTitlePramas = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
tvTitle.setGravity(CENTER);
tvTitle.setLayoutParams(tvTitlePramas);
tvTitle.setText("我是标题");
setTextColor(tvTitle, R.color.white);
setTextSize(tvTitle, 20);
//-------------------------------------------设置返回按钮位置等信息-----------------------------------------
imgFinish.setScaleType(ImageView.ScaleType.CENTER);//设置图片为居中位置
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);
//-----------------------------------------设置右侧文字内容------------------------------------------------
RelativeLayout.LayoutParams tvDescribeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tvDescribeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//设置位于最右边
tvDescribeParams.addRule(RelativeLayout.CENTER_VERTICAL);//设置位于最右边
tvDescribeParams.setMargins(0, 0, dp15, 0);//设置右外边距
tvDescribe.setLayoutParams(tvDescribeParams);
tvDescribe.setText("描述");
setTextColor(tvDescribe, R.color.white);
setTextSize(tvDescribe, 14);
//----------------------------------------设置右侧图片位置--------------------------------------------
RelativeLayout.LayoutParams imgMoveParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imgMoveParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//设置位于最右边
imgMoveParams.addRule(RelativeLayout.CENTER_VERTICAL);//设置位于最右边
imgMoveParams.setMargins(0, 0, dp15, 0);//设置右外边距
imgMove.setScaleType(ImageView.ScaleType.CENTER);
imgMove.setLayoutParams(imgMoveParams);
//设置整体
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);
});
}
/**
* 设置标题栏风格
*
* @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);//设置右外边距
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());//设置位于最右边
tvDescribeParams.addRule(RelativeLayout.CENTER_VERTICAL);//设置位于最右边
tvDescribeParams.setMargins(0, 0, dp15, 0);//设置右外边距
tvDescribe.setLayoutParams(tvDescribeParams);
}
break;
}
}
/**
* 获取状态栏高度
*
* @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);
}
/**
* 设置标题
*
* @param title
*/
public void setTitleText(String title) {
tvTitle.setText(title);
}
public String getTitleText() {
return tvTitle.getText().toString();
}
/**
* 设置描述
*
* @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);
}
}
以上就是标题栏的全部代码,当然也有自定义属性如下:
当然是用起来也是相当简单的:
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="注册"
app:titlebar_style="finish_title_move_text" />
还有很多功能不一一演示了。
分享出来,希望能帮助很多同事,减少一部分工期时间,如果有漏洞或是性能问题,请与我联系,谢谢。