写个自定义控件时经常要自定义一些自己的属性,平时用的都是那几个,今天就顺便一起总结一下这个东东吧~
一、定义:属性的定义都在attrs.xml文件里面;
二、读取:通过都是通过TypedArray去读取的,要获取TypedArray都是通过context.obtainStyledAttributes去获取的,它有几个重载方法,一般形如: TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
三、使用:要使用自定义属性,得先在布局文件声明 xmlns:app="http://schemas.android.com/apk/res-auto" 当然,你不喜欢app也可以自定义名字,形如:xmlns:custom="http://schemas.android.com/apk/res/{packagename}"。
四、自定义format的概览:
format名称
format类型
reference
表示引用,参考某一资源ID
string
表示字符串
color
表示颜色值
boolean
表示尺寸值
dimension
表示布尔值
float
表示浮点值
integer
表示整型值
fraction
表示百分数
enum
表示枚举值
flag