3.5Android控件架构与自定义控件详解之自定义View(三)
2016-03-22 20:36
656 查看
3.5.2 创建复合控件
创建复合空间可以很好的创建出具有重用功能的空间集合,这种方式通常需要继承一个合适的ViewGroup,在给它添加指定功能的控件,从而组合成新的复合控件。以TopBar为例,标题栏
需要创一个UI模版,还要有丰富的接口,可以改变模版的文字颜色行为等信息。
3.5.2.1 定义属性
res资源目录下的Values文件夹创一个attrs.xml的属性定义文件,代码如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TopBar"> <attr name="title" format="string" /> <attr name="titleTextSize" format="string" /> <attr name="titleTextColor" format="color" /> <attr name="lefeTextColor" format="color" /> <attr name="leftBackground" format="reference" /> <attr name="leftText" format="string" /> <attr name="rightTextColor" format="color" /> <attr name="rightBackground" format="reference" /> <attr name="rightText" format="string" /> </declare-styleable> </resources>
自定义如下:
private int leftTextColor; private Drawable leftBackground; private String leftText; private float titleTextSize; private int titleTextColor; private String titleText; private int rightTextColor; private Drawable rightBackground; private String rightText; private Button mLeftButton, mRightButton; private TextView mTitleText; private LayoutParams leftLayoutParams, TitleLayoutParams, rightLayoutParams; private myTopBarClickListener myTopBarClickListener; //自定义click监听回调 public interface myTopBarClickListener { public void leftClick(); public void rightClick(); } //自定义一个监听事件方法 public void setonTopBarClickListener(myTopBarClickListener myTopBarClickListener) { this.myTopBarClickListener = myTopBarClickListener; } public MyTopBar(Context context) { super(context); } public MyTopBar(Context context, AttributeSet attrs) { super(context, attrs); //通过这个方法,将你在attrs.xml中自定义的declare-styleable的所有属性的值存放在TypedArray中 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar); //从TypedArray中获取对应的值来为要设置的属性赋值 leftTextColor = typedArray.getColor(R.styleable.TopBar_lefeTextColor, 0); leftBackground = typedArray.getDrawable(R.styleable.TopBar_leftBackground); leftText = typedArray.getString(R.styleable.TopBar_leftText); rightTextColor = typedArray.getColor(R.styleable.TopBar_rightTextColor, 0); rightBackground = typedArray.getDrawable(R.styleable.TopBar_rightBackground); rightText = typedArray.getString(R.styleable.TopBar_rightText); titleTextColor = typedArray.getColor(R.styleable.TopBar_titleTextColor, 0); titleText = typedArray.getString(R.styleable.TopBar_title); titleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize, 10); //获取完TypedArray的值后,一般要调用recyle方法来避免重新创建的时候的错误 //完成资源的回收 typedArray.recycle(); //动态添加控件的方式 mLeftButton = new Button(context); mRightButton = new Button(context); mTitleText = new TextView(context); mRightButton.setTextColor(rightTextColor); mRightButton.setBackgroundDrawable(rightBackground); mRightButton.setText(rightText); mLeftButton.setText(leftText); mLeftButton.setTextColor(leftTextColor); mLeftButton.setBackgroundDrawable(leftBackground); mTitleText.setText(titleText); mTitleText.setTextColor(titleTextColor); mTitleText.setTextSize(titleTextSize); mTitleText.setGravity(Gravity.CENTER); //组件元素设置相应的布局元素 leftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); //添加到ViewGroup中 addView(mLeftButton, leftLayoutParams); rightLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); addView(mRightButton, rightLayoutParams); TitleLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); TitleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); addView(mTitleText, TitleLayoutParams); //设置监听事件 mLeftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myTopBarClickListener.leftClick(); } }); mRightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myTopBarClickListener.rightClick(); } }); } //设置Button是否显示 public void setLeftIsVisible(boolean flag) { if (flag) { mLeftButton.setVisibility(View.VISIBLE); } else { mLeftButton.setVisibility(View.GONE); } } public void setRightIsVisible(boolean flag) { if (flag) { mRightButton.setVisibility(View.VISIBLE); } else { mRightButton.setVisibility(View.GONE); } }
还有非常重要的是引用UI模版,
Android studio 中用如下代码:
xmlns:custom="http://schemas.android.com/apk/res-auto"这样我们就能引用自定义的TopBar了
更方便的是,我们可以将这个UI模版写到一个布局文件中,然后打上<include>的标签。
相关文章推荐
- 自定义一个TopBar(完整流程)
- 自定义topbar
- 3.5.Android控件架构与自定义控件详解之自定义View(二)
- 博客网址
- 使用jenkins配置.net mvc网站进行持续集成三
- 高可用rabbitmq集群服务部署步骤
- 一个神奇的网站(快快乐乐写时序图)
- 《软件架构设计》学习笔记--3--软件架构视图
- LVS负载均衡之lvs高可用实例部署2(案例篇)
- VS2015 使用 Web Deploy 发布网站到 WindowsServer2008 R2服务器详解
- 《软件架构设计》学习笔记--2--软件架构概念
- 一些资源网站
- 用 Express 4.x 搭建项目 —— 后台架构
- 大型分布式系统架构中的“let it crash”思想
- 《软件架构设计》学习笔记--1--阅读感悟
- 细谈普通网站的后台构建实战----my note
- 细谈构建高性能的网站架构以及名站架构分析一览------my note
- 高性能的智能日志
- 知乎架构
- 如何用代码设置控制自己网站的网页在360浏览器打开时强制优先使用极速模式,而非兼容模式