简单三步通过Builder模式来实现Android顶部导航TopBar
16lz
2021-01-23
作为一个Android开发人员在开发的时候,经常会对APP不同页面的Topbar\ActionBar进行设置,
这是Topbar左中右三个控件的属性,通过set方法,来调用设置:
// 整个topbar的背景色,默认是橘黄色 private int backgaroundColor = 0xFFF59563; // 声明Topbar所需要的控件 private TextView tvLeft, tvRight;//左\右 private TextView tvTitle;//中 // 声明Topbar控件所需要用到的属性 // leftImageButton private int leftTextColor;//字体颜色 private Drawable leftBackground;//背景图片 private float leftTextSize;//字体大小 private String leftText;//文字内容 // rightImageButton private int rightTextColor;//颜色 private Drawable rightBackground;//背景图片 private String rightText;//文字内容 private float rightTextSize;//字体大小 // tvTitle private String title;//文字内容 private float titleTextSize;//字体大小 private int titleTextColor;//字体颜色 private topbarClickListener listener;//左\右点击事件监听
一般都是在所属activity通过get\set方法进行相关属性的设置,代码如下:
// topbar.setBackgaroundColor(Color.GRAY);//整个topbar的背景色,有默认颜色,根据实际需求修改 topbar.setLeftBackground(getResources().getDrawable(R.drawable.shezhi_03));//设置左边按键为图片// topbar.setLeftText("退出");//设置左边按键为文字// topbar.setLeftTextColor(Color.RED);//设置左边文字颜色,默认黑色// topbar.setLeftTextSize(12);//设置左边文字大小,默认10sp topbar.setRightBackground(getResources().getDrawable(R.drawable.xg));//设置右边按键为图片// topbar.setRightText("保存");//设置右边按键为文字// topbar.setRightTextColor(Color.RED);//设置左边文字颜色,默认黑色// topbar.setRightTextSize(12);//设置左边文字大小,默认10sp topbar.setTitle("首页"); topbar.setOnTopbarClickListener(new topbarClickListener() { @Override public void rightClick() { Toast toast=Toast.makeText(MainActivity.this,"右边",Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER,0,0); toast.show(); } @Override public void leftClick() { Toast toast=Toast.makeText(MainActivity.this,"左边",Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER,0,0); toast.show(); } });
但随着APP的页面增多,并且如果存在复杂的Topbar,那么用这种常规的get\set方式来初始化Topbar,代码效率和可维护性便大大降低了.在这种情况,用builder模式来实现不同Topbar的初始化就显得很合适了.首先来看看通过builder模式来初始化Topbar对象的代码:
Topbar.Builder builder = new Topbar.Builder(); topbar = builder.context(MainActivity.this)//配置相关属性 .backgaroundColor(Color.BLACK) .title("中间") .titleTextColor(Color.BLACK). titleTextSize(12) .topbarClickListener(new topbarClickListener() { @Override public void rightClick() { //这里写右边button的点击事件 } @Override public void leftClick() { //这里写左边button的点击事件 } }).build();
看上去是不是简洁了许多呢?
好了,接下来说下实现方法,很简单,只需要在原来基础上增加简单三步.
实现步骤:
1.在Topbar类里面构建静态内部类Builder,(1)和topbar具有相同的属性参数,(2)和返回builder实例对象public的方法,代码如下:
//和topbar具有相同的属性值,和public返回该builder实例的方法public static class Builder{ // 整个topbar的背景色,默认是橘黄色 private int backgaroundColor = 0xFFF59563; private Context context; // 声明Topbar控件所需要用到的属性 // leftImageButton private int leftTextColor;//字体颜色 private Drawable leftBackground;//背景图片 private float leftTextSize;//字体大小 private String leftText;//文字内容 // rightImageButton private int rightTextColor;//颜色 private Drawable rightBackground;//背景图片 private String rightText;//文字内容 private float rightTextSize;//字体大小 // tvTitle private String title;//文字内容 private float titleTextSize;//字体大小 private int titleTextColor;//字体颜色 private topbarClickListener listener;//左\右点击事件监听 public Builder context(Context context){ this.context = context; return this; } public Builder topbarClickListener(topbarClickListener listener){ this.listener = listener; return this; } public Builder backgaroundColor(int backgaroundColor){ this.backgaroundColor = backgaroundColor; return this; } public Builder leftTextColor(int leftTextColor){ this.leftTextColor = leftTextColor; return this; } public Builder leftBackground(Drawable leftBackground){ this.leftBackground = leftBackground; return this; } public Builder leftTextSize(float leftTextSize){ this.leftTextSize = leftTextSize; return this; } public Builder leftText(String leftText){ this.leftText = leftText; return this; } public Builder rightTextColor(int rightTextColor){ this.rightTextColor = rightTextColor; return this; } public Builder rightBackground(Drawable rightBackground){ this.rightBackground = rightBackground; return this; } public Builder rightText(String rightText){ this.rightText = rightText; return this; } public Builder rightTextSize(float rightTextSize){ this.rightTextSize = rightTextSize; return this; } public Builder title(String title){ this.title = title; return this; } public Builder titleTextSize(float titleTextSize){ this.titleTextSize = titleTextSize; return this; } public Builder titleTextColor(int titleTextColor){ this.titleTextColor = titleTextColor; return this; } public Topbar build(){ return new Topbar(context, this);//这个是第三步的,最终返回topbar实例对象 } }
2.在Topbar里面申明一个带Builder参数的构造方法,代码如下:
public Topbar(Context context, Builder builder) { super(context); this.context = context; builder.context = context; this.backgaroundColor = builder.backgaroundColor; this.leftTextColor = builder.leftTextColor; this.leftBackground = builder.leftBackground; this.leftTextSize = builder.leftTextSize; this.leftText = builder.leftText; this.rightTextColor = builder.rightTextColor; this.rightBackground = builder.rightBackground; this.rightText = builder.rightText; this.rightTextSize = builder.rightTextSize; this.title = builder.title; this.titleTextSize = builder.titleTextSize; this.titleTextColor = builder.titleTextColor; this.listener = builder.listener; }
3.在静态内部类Builder通过build方法来返回Topbar,代码如下:
public Topbar build(){ return new Topbar(context, this); }
最后则是我们的源码demo,里面有除了topbar的源码,还有个简单的builder模式实现案例的demo,相信看了一遍的同学,很快就能自己通过builder模式构建复杂的对象了~~,
点击下载.
http://download.csdn.net/detail/qq_28690547/9398303
更多相关文章
- Android字体跟颜色
- 中秋深夜码字,完成了一个底部导航栏(Android自定义控件),一键添加
- Android TextView部分文字实现点击事件
- TextView显示文字过长时添加阴影渐变消失效果
- android 引导用户指示操作 高亮显示 可以自定义文字或者图片来作