Android中的style,attr,theme

来源:互联网 发布:锁链战记wiki数据库 编辑:程序博客网 时间:2024/05/17 00:13
在通过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属性值。


总结下,免得每次忘

1)style类似于css,用来定义View等UI元素的属性,比如layout_width等,可以在layout的xml中用@style/name来引用

2)style中如果要加入自定义属性,可以在attr中定义,一个attr包括name和format属性,可以在style中用@attr/name来引用

      也可以在attr中定义属性集合,用declare-styleable标签定义,这个属性集合在view的构造函数中用到

      public View(Context context, AttributeSet attrs, int defStyle);

     View就可以在构造函数中知道要去defStyle中的哪些属性出来

     属性集的引用不同于单个属性,使用R.styleable.name来引用

3)theme一般是针对activity的,全局生效,一般在theme中指定style,而这个指定,一般由一个属性来完成,属性在attr中定义

比如attr中定义autoCompletTextViewStyle属性,然后在theme中指定

<item name="autoCompleteTextViewStyle">@android:style/Widget.AutoCompleteTextView</item> 

这样就可以在View的构造函数中,利用R.attr.autoCompleteTextViewStyle取得Theme中指定的style


在 public View(Context context, AttributeSet attrs, int defStyle);

中,defStyle既可以是某一个style:R.style.name,也可以是R.attr.name,对于后者,需要在theme中指定属性值。






0 0
原创粉丝点击