自定义View的属性,首先在res/values/ 下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式。
如:
<pre name="code" class="java"> <?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myTextType" format="string" />
<attr name="myTextColor" format="color" />
<attr name="myTextSize" format="dimension" />
<declare-styleable name="CustomTitleView">
<attr name="myText" />
<attr name="myTextColor" />
<attr name="myTextSize" />
</declare-styleable>
</resources>
如上代码:
我们自定义了三个属性 名字分别为字体类型,字体颜色,字体大小,以及指定了属性值类型,分别为String, color,dimension;
属性取值类型有这几种
- reference
- color
- boolean
- dimension
- float
- integer
- string
- fraction
- enum - normally implicitly defined
- flag - normally implicitly defined
下面分别讲解这几个类型:
已下资料来自http://www.cnblogs.com/qinghuaideren/p/3495906.html
一:reference:引用资源ID,如图片,风格引用,String引用。
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable> <ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID"/>
二:color:颜色值,如#00000000透明色。
<declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable> <T