1.为什么要使用自定义属性?
由于需求的不满足
2.怎样自定义属性?
第一种:我们并不知道系统定义了此名称的属性,我们自己定义一个名为text或者mText的属性(属性名称可以随便起的)
<resources> <declare-styleable name="MyTextView"> <attr name=“text" format="string" /> </declare-styleable></resources>
第二种:我们知道系统已经定义过名称为text的属性,我们不用自己定义,只需要在自定义属性中申明,我要使用这个text属性(注意加上android命名空间,这样才知道使用的是系统的text属性)
<resources> <declare-styleable name="MyTextView"> <attr name=“android:text"/> </declare-styleable></resources>
3.属性值的类型format
format一共支持的类型一共有11种
(1). reference:参考某一资源ID
- 属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /></declare-styleable>
- 属性使用:
<ImageView android:background = "@drawable/图片ID"/>
(2). color:颜色值
- 属性定义:
<attr name = "textColor" format = "color" />
- 属性使用:
<TextView android:textColor = "#00FF00" />
(3). boolean:布尔值
- 属性定义:
<attr name = "focusable" format = "boolean" />
- 属性使用:
<Button android:focusable = "true"/>
(4). dimension:尺寸值
- 属性定义:
<attr name = "layout_width" format = "dimension" />
- 属性使用:
<Button android:layout_width = "42dip"/>
(5). float:浮点值
- 属性定义:
<attr name = "fromAlpha" format = "float" />
- 属性使用:
<alpha android:fromAlpha = "1.0"/>
(6). integer:整型值
- 属性定义:
<attr name = "framesCount" format="integer" />
- 属性使用:
<animated-rotate android:framesCount = "12"/>
(7). string:字符串
- 属性定义:
<attr name = "text" format = "string" />
- 属性使用:
<TextView android:text = "我是文本"/>
(8). fraction:百分数
- 属性定义:
<attr name = "pivotX" format = "fraction" />
- 属性使用:
<rotate android:pivotX = "200%"/>
(9). enum:枚举值
- 属性定义:
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr></declare-styleable>
- 属性使用:
<LinearLayout android:orientation = "vertical"></LinearLayout>
注意:枚举类型的属性在使用的过程中只能同时使用其中一个,不能 android:orientation = “horizontal|vertical"
(10). flag:位或运算
- 属性定义:
<declare-styleable name="名称"> <attr name="gravity"> <flag name="top" value="0x30" /> <flag name="bottom" value="0x50" /> <flag name="left" value="0x03" /> <flag name="right" value="0x05" /> <flag name="center_vertical" value="0x10" /> ... </attr></declare-styleable>
- 属性使用:
<TextView android:gravity="bottom|left"/>
注意:位运算类型的属性在使用的过程中可以使用多个值
(11). 混合类型:属性定义时可以指定多种类型值
- 属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /></declare-styleable>
- 属性使用:
<ImageViewandroid:background = "@drawable/图片ID" />或者:<ImageViewandroid:background = "#00FF00" />
4.类中获取属性值
如果我们自定义属性,这个属性应该去我们的应用程序包中找,所以要引入我们应用包的命名空间
xmlns:openxu="http://schemas.android.com/apk/res-auto",res-auto表示自动查找,还有一种写法
xmlns:openxu="http://schemas.android.com/apk/com.example.openxu.myview",
com.example.openxu.myview为我们的应用程序包名。
5.在构造方法中获取属性值:
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); String text = ta.getString(R.styleable.MyTextView_android_text); int mTextColor = ta.getColor(R.styleable.MyTextView_mTextColor, Color.BLACK); int mTextSize = ta.getDimensionPixelSize(R.styleable.MyTextView_mTextSize, 100); ta.recycle(); //注意回收
6.PS
如果我们写了
declare-styleable,在R文件中就会生成
styleable类,这个类其实就是将每个控件的属性分组,然后记录属性的索引值,而TypedArray正好需要通过此索引值获取属性。