您的位置:首页 > 其它

对AttributeSet和defStyle的理解

2014-11-03 15:30 197 查看
在通过xml文件构造view组件的时候,往往都要使用到AttributeSet和defStyle这个两个参数,例如Button组件的构造方法Button(Context ctx, AttributeSet attrs, int defStyle)中,ctx会调用obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)方法获得一个TypedArray,然后根据这个TypeArray来设置组件的属性。obtainStyledAttributes这类方法有好几个,真正的实现是Resources.Theme类,分别是:

(1) obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) : TypedArray

(2) obtainStyledAttributes( int resid, int[] attrs) : TypeArray
(3) obtainStyledAttributes(int[] attrs) : TypeArray

在方法(1)里根据attrs确定要获取哪些属性,然后依次通过其余3个参数来取得相应的属性值,属性值获取的优先级从高到低依次是set, defStyleAttr, defStyleRes. defStyleAttr是一个reference, 它指向当前Theme中的一个style, style其实就是各种属性的集合,如果defStyleAttr为0或者在Theme中没有找到相应的style, 则 才会尝试从defStyleRes获取属性值,defStyleRes表示的是一个style的id, 当它为0时也无效。方法(2)和(3)分别表示从style或Theme里获取属性值。

attr是在/res/values/attrs.xml文件下定义的,除了系统组件本身的属性,我们也可以自定义属性,然后在layout布局中使用。attrs.xml里通常包括若干个attr集合,例如

<declare-styleable name="LabelView">

<attr name="text" format="string" />

<attr name="textColor" format="color" />

<attr name="textSize" format="dimension" />
</declare-styleable>

就表示一个attr集合,declare-styleable标签里的name值表示的就是上面方法里的attrs参数,android会自动在R文件中生成一个数组, 它可以使任意的不一定要是view组件名称。在集合里定义每个属性的名称和它的类型,据偶所见总共有reference, string, color, dimension, boolean等,如果允许多个类型可以用"|"来隔开,比如reference | color, attr还可以这样定义

<attr name="layout_height" format="dimension">

<enum name="fill_parent" value="-1" />

<enum name="match_parent" value="-1" />

<enum name="wrap_content" value="-2" />
</attr>

当attr的定义没有指明format时,表示它已经在其他地方定义过了,所以你可以定义一个attr集合,里面的都是已经定义好的属性(例如系统组件的属性), 然后通过obtainStyledAttributes方法来获取这些属性值,例如

<declare-styleable name="Gallery1">

<attr name="android:galleryItemBackground" />
</declare-styleable>

在layout布局中使用自定义的属性,要指明包名称,需要先定义,例如xmlns:app="http://schemas.android.com/apk/res/your_package_name", 然后就可以这样app:text, app:textSize来设置属性了。

R文件中会有styleable和attr这两个类,当我们要使用哪个属性集合或哪个属性的时候用的是styleable, 而attr类定义的仅仅是attr这个属性在layout中的id. AttributeSet有两个方法分别是

int getAttributeNameResource(int index);

int getAttributeResourceValue(int index, int defaultValue);

前一个方法获取的就是attr属性名称的id,也也就是attr类定义的数值,后一个方法获取的才是attr属性值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AttributeSet defStyle