每个App里面都少不了标题栏,我们一般怎么使用的呢?大部分都是在布局文件中去写布局,或者是使用ActionBar,ToolBar,有些时候,总是达不到我们的要求,因此我就和大家分享一下自己定制的一款万能的通用的标题栏,我们采用的是Builder设计模式,同时也是模仿系统Dialog的Builder设计模式来写的。
我们先看一下效果:
首先定义一个接口,用来规范:
public interface ITitleBar {
// 头部的规范
public int bindLayoutId();
// 绑定头部的参数
public void applyView();
}
private P mParams;
private View mTitleBarView;
public BaseTitleBar(P params) {
this.mParams = params;
createAndBindView();
}
/**
* 设置文本
* @param viewId
* @param text
*/
protected void setText(int viewId, String text) {
TextView tv = findViewById(viewId);
if(!TextUtils.isEmpty(text)){
tv.setVisibility(View.VISIBLE);
tv.setText(text);
}
}
/**
* 标题栏颜色设置
* @param viewId
* @param titleBarColor
*/
protected void setTitleBarColor(int viewId, int titleBarColor) {
RelativeLayout relativeLayout = findViewById(viewId);
if(titleBarColor!=0){
relativeLayout.setBackgroundColor(mParams.mContext.getResources().getColor(titleBarColor));
}
}
/**
* 设置文字颜色
* @param viewId
* @param textColor
*/
protected void setTextColor(int viewId, int textColor) {
TextView tv = findViewById(viewId);
if(textColor!=0){
tv.setTextColor(mParams.mContext.getResources().getColor(textColor));
}
}
/**
* 设置icon
* @param viewId
* @param res
*/
protected void setIcon(int viewId,int res){
ImageView iv = findViewById(viewId);
if (res!=0){
iv.setVisibility(View.VISIBLE);
iv.setImageResource(res);
}
}
/**
* 设置点击
* @param viewId
* @param listener
*/
protected void setOnClickListener(int viewId,View.OnClickListener listener){
findViewById(viewId).setOnClickListener(listener);
}
/**
* 绑定和创建View
*/
private void createAndBindView() {
//创建View
if(mParams.mParent == null){
// 获取activity的根布局
ViewGroup activityRoot = (ViewGroup) ((Activity)(mParams.mContext))
.findViewById(android.R.id.content);
mParams.mParent = (ViewGroup) activityRoot.getChildAt(0);
}
if(mParams.mParent == null){
return;
}
mTitleBarView = LayoutInflater.from(mParams.mContext).
inflate(bindLayoutId(), mParams.mParent, false);
//添加
mParams.mParent.addView(mTitleBarView, 0);
applyView();
}
public <T extends View> T findViewById(int viewId){
return (T)mTitleBarView.findViewById(viewId);
}
public P getParams() {
return mParams;
}
// Builder仿照系统写的dialo就是这样,参数Params
public abstract static class Builder {
public Builder(Context context, ViewGroup parent) {
}
public abstract BaseTitleBar builder();
public static class BaseTitleBarParams {
public Context mContext;
public ViewGroup mParent;
public BaseTitleBarParams(Context context, ViewGroup parent) {
this.mContext = context;
this.mParent = parent;
}
}
}
下面是我们的自己的标题栏
public CommonTitleBar(CommonTitleBar.Builder.CommonTitleBarParams params) {
super(params);
}
@Override
public int bindLayoutId() {
return R.layout.title_bar;
}
@Override
public void applyView() {
// 绑定效果
setTitleBarColor(R.id.title_bar, getParams().mTitleBarColor);
setText(R.id.left_tv, getParams().mLeftText);
setTextColor(R.id.left_tv, getParams().mLeftTextColor);
setIcon(R.id.left_icon, getParams().mLeftIconRes);
setText(R.id.title, getParams().mTitle);
setTextColor(R.id.title, getParams().mTitleColor);
setIcon(R.id.title_icon, getParams().mTitleIconRes);
setText(R.id.right_text, getParams().mRightText);
setTextColor(R.id.right_text, getParams().mRightTextColor);
setIcon(R.id.right_icon, getParams().mRightIconRes);
// 左边 要写一个默认的 finishActivity
setOnClickListener(R.id.left_icon, getParams().mLeftClickListener);
setOnClickListener(R.id.left_tv, getParams().mLeftClickListener);
setOnClickListener(R.id.title, getParams().mCenterClickListener);
setOnClickListener(R.id.title_icon, getParams().mCenterClickListener);
setOnClickListener(R.id.right_text, getParams().mRightClickListener);
setOnClickListener(R.id.right_icon, getParams().mRightClickListener);
}
public static class Builder extends BaseTitleBar.Builder {
CommonTitleBarParams P;
public Builder(Context context, ViewGroup parent) {
super(context, parent);
P = new CommonTitleBarParams(context, parent);
}
public Builder(Context context) {
super(context, null);
P = new CommonTitleBarParams(context, null);
}
@Override
public CommonTitleBar builder() {
CommonTitleBar commonTitleBar = new CommonTitleBar(P);
return commonTitleBar;
}
//设置所有效果
/**
* 标题栏颜色设置
* @param color
* @return
*/
public CommonTitleBar.Builder setTitleBarColor(int color) {
P.mTitleBarColor = color;
return this;
}
/**
* 设置左边文字
*
* @param leftText
* @return
*/
public CommonTitleBar.Builder setLeftText(String leftText) {
P.mLeftText = leftText;
return this;
}
/**
* 设置左边文字颜色
*
* @param color
* @return
*/
public CommonTitleBar.Builder setLeftTextColor(int color) {
P.mLeftTextColor = color;
return this;
}
/**
* 设置左边的图片
*/
public CommonTitleBar.Builder setLeftIcon(int leftIconRes) {
P.mLeftIconRes = leftIconRes;
return this;
}
/**
* 设置标题
*
* @param title
* @return
*/
public CommonTitleBar.Builder setTitle(String title) {
P.mTitle = title;
return this;
}
/**
* 设置标题颜色
*
* @param color
* @return
*/
public CommonTitleBar.Builder setTitleColor(int color) {
P.mTitleColor = color;
return this;
}
/**
* 设置中间的图标
*
* @param titleIconRes
* @return
*/
public CommonTitleBar.Builder setTitleIcon(int titleIconRes) {
P.mTitleIconRes = titleIconRes;
return this;
}
/**
* 设置右边文字
*
* @param rightText
* @return
*/
public CommonTitleBar.Builder setRightText(String rightText) {
P.mRightText = rightText;
return this;
}
/**
* 设置右边文字颜色
*
* @param color
* @return
*/
public CommonTitleBar.Builder setRightTextColor(int color) {
P.mRightTextColor = color;
return this;
}
/**
* 设置右边的图片
*/
public CommonTitleBar.Builder setRightIcon(int rightRes) {
P.mRightIconRes = rightRes;
return this;
}
/**
* 设置左边的点击事件
*/
public CommonTitleBar.Builder
setLeftClickListener(View.OnClickListener leftListener) {
P.mLeftClickListener = leftListener;
return this;
}
/**
* 设置中间的点击事件
*/
public CommonTitleBar.Builder setTitleClickListener(View.OnClickListener titleListener) {
P.mCenterClickListener = titleListener;
return this;
}
/**
* 设置右边的点击事件
*/
public CommonTitleBar.Builder setRightClickListener(View.OnClickListener rightListener) {
P.mRightClickListener = rightListener;
return this;
}
public static class CommonTitleBarParams extends
BaseTitleBar.Builder.BaseTitleBarParams {
// 所有效果放置
public String mLeftText;//左边文字
public int mLeftTextColor;//左边文字颜色
public int mLeftIconRes;//左边图片
public int mTitleBarColor;//标题栏颜色
public String mTitle;//标题
public int mTitleColor;//标题颜色
public int mTitleIconRes;//标题图片
public String mRightText;//右边文字
public int mRightTextColor;//右边文字颜色
public int mRightIconRes;//右边图片
// 后面还有一些通用的
public View.OnClickListener mRightClickListener;
public View.OnClickListener mCenterClickListener;
public View.OnClickListener mLeftClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// 关闭当前Activity
((Activity) mContext).finish();
}
};
public CommonTitleBarParams(Context context, ViewGroup parent) {
super(context, parent);
}
}
}
主要功能:
* CommonTitleBar使用,分别有左图标,左边文字 * 中间标题 中间图标 * 右边图标 右边文字 * 不设置文字或者不设置图片资源 图表或者文字为gone * 左边图标默认设置了点击事件为关闭当前页面 根据需要进行处理
使用:
CommonTitleBar titleBar = new CommonTitleBar.Builder(this) .setTitleBarColor(R.color.blue) .setLeftText("左边标题") .setLeftTextColor(R.color.red) .setLeftIcon(R.drawable.iv_back_arrow_white) .setLeftClickListener(this) .setTitle("投稿") .setTitleColor(R.color.white) .setTitleIcon(R.drawable.iv_mine_on) .setTitleClickListener(this) .setRightText("分享") .setRightTextColor(R.color.colorAccent) .setRightIcon(R.drawable.iv_more_blue) .setRightClickListener(this) .builder();
无需在布局中去加载既可以用,附上github地址:点击打开链接