欢迎使用CSDN-markdown编辑器
2016-01-23 14:33
274 查看
自定义view之组合控件
自定义属性
创建attr.xml文件,自定义属性,格式如下:<?xml version="1.0" encoding="utf-8"?> <resources> <!--自定义标题栏属性--> <declare-styleable name="topbar"> <attr name="middleText" format="string"></attr> <attr name="middleTextSize" format="dimension"></attr> <attr name="middleTextColor" format="color"></attr> <attr name="leftTextColor" format="color"></attr> <attr name="leftBackground" format="reference|color"></attr> <attr name="leftText" format="string"></attr> <attr name="rightTextColor" format="color"></attr> <attr name="rightBackground" format="reference|color"></attr> <attr name="rightText" format="string"></attr> </declare-styleable> </resources>
declare-styleable是添加自定义属性用的;
每一个item是一条属性,name表示属性名,format表示属性的格式:
string
dimension
color
reference
reference|color
…
可以两个一起使用,如上面的background既可以是资源也可以是颜色;
2.在要引用的xml里声明命名空间,并引用
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.toolbar.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout>
上面的根布局里声明了xmlns:app=”http://schemas.android.com/apk/res-auto”,app这个名字.控件里只需要app:popupTheme=”@style/AppTheme.PopupOverlay”就行.
3.自定义view里给自定义属性赋予功能
-
列表内容
在自定义view的构造里取出我们自定义的属性(通用)
/**
* 取出我们自定义的属性
*/
//通过这个方法获取我们自定义的属性和xml里使用的属性
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.topbar);
//从ta中取出对应的属性并赋值
//参数一:对应的属性;参数二:默认值
leftTextColor=ta.getColor(R.styleable.topbar_leftTextColor, 0);
leftBackground=ta.getDrawable(R.styleable.topbar_leftBackground);
leftText=ta.getString(R.styleable.topbar_leftText);
rightTextColor=ta.getColor(R.styleable.topbar_rightTextColor, 0); rightBackground=ta.getDrawable(R.styleable.topbar_rightBackground); rightText=ta.getString(R.styleable.topbar_rightText); middleTextSize=ta.getFloat(R.styleable.topbar_middleTextSize, 0); middleTextColor=ta.getColor(R.styleable.topbar_middleTextColor, 0); middleText=ta.getString(R.styleable.topbar_middleText); /** * 注意:在使用完TypedArray之后,要recycle()一下 * 1.避免耗费资源; * 2.避免缓存可能引起的一些问题 */ ta.recycle();
情况一:如果是组合控件的话,直接把属性设置到对应的控件的对应属性上去;
情况二:如果纯绘制控件的话,根据具体情况设置属性;
eg:情况一
//在构造中实例化组合控件中的各控件 leftButton=new Button(context); rightButton=new Button(context); middleTextView=new TextView(context); /** * 接下来把自定义属性的值赋值给各自控件的已有属性; */ leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftBackground); leftButton.setText(leftText);
回调写法(通用)
原理:首先是调用者通过这个方法把接口赋给我们自身的接口;然后就是在我们自身类中用到的地方调用接口中的方法;
创建接口和接口方法
public interface topBarClickListener{ //可以一个接口两个不同的接口方法 public void leftClick(); public void rightClick(); }
创建设置接口的set方法,接收调用者传递过来的接口
public void setTopBarCliclListener(topBarClickListener topBarCliclListener){
this.topBarClickListener=topBarClickListener;
}
3.在需要使用的地方调用接口方法
注意:同一个接口内可以有多个接口方法
示例-自定义通用标题栏
相关文章推荐
- 如何修改自增列值以及相应的解决方法
- React-Native 开发 android & ios App,共享一份代码
- 0003-如何查找01的位置
- UVA213----Message Decoding
- 10026 - Shoemaker's Problem
- 2015中国城市GDP排名出炉
- HDU 1709 The Balance
- rdd
- Asp.Net MVC3 简单入门详解过滤器Filter
- 0002-Testfs 怎么建立
- Fragment的知识
- Java将GeoHash转化为对应的经纬度坐标实例代码
- Android 几个学习用到的网址
- java中的反射
- Swift快速入门(一)第一个Swift程序
- Swift快速入门(一)第一个Swift程序
- Cookie 和 Session机制详解
- 2015年总结与2016年目标
- 「C语言」「概念」数据类型及混合运算与类型转换
- UI_炫彩方框,旋转跑马灯,炫彩画板