今天總結一下自定義 View 時自定義屬性的相關。
定義
在 res/values 中創(chuàng)建 attrs.xml 文件。
示例代碼:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="custom_color" format="color"/>
<declare-styleable name="CustomAttribute">
<attr name="custom_radius" format="dimension"/>
<attr name="custom_color"/>
</declare-styleable>
</resources>
- attr 標簽定義一個屬性,name 是屬性的名字,不能和其它屬性的名字沖突,format 是屬性的格式,可以用給一個屬性指定多種格式;
- declare-styleable 標簽定義一個屬性組,可以在里面定義屬性,也可以直接引用 resource 標簽下面定義的屬性,區(qū)別就是不用寫 format;
格式
自定義屬性共有10種 format
- integer 整型值;
- float 浮點值;
- string 字符串;
- boolean 布爾值;
- dimension 尺寸值;
- color 顏色值;
- fraction 百分數;
- enum 枚舉值;
- flag 位或運算
- reference 引用資源 ID
獲取
屬性的獲取有兩種方式:
獲取 resource 下的 attr
第一種是直接獲取 resource 標簽下定義的 attr。
每一個 attr 都會在 R 文件的 attr 類中生成一個對應 ID,我們可以根據這個 ID 獲取自己定義的屬性,也可以獲取系統(tǒng)定義的屬性。
通常是在 View 的構造方法中獲取:
//獲取自定義屬性
int[] customAttrs = {R.attr.custom_color};
TypedArray a = context.obtainStyledAttributes(attrs, customAttrs);
int color = a.getColor(0, Color.WHITE);
a.recycle();
//獲取系統(tǒng)定義屬性
int[] customAttrs = {android.R.attr.color};
TypedArray a = context.obtainStyledAttributes(attrs, customAttrs);
mColor = a.getColor(0, mColor);
a.recycle();
獲取 declare-styleable 下的 attr
更常用的是獲取 declare-styleable 屬性集中的屬性。
當定義 declare-styleable 時,R 文件在 styleable 內部類中自動生成一個 int[] 常量,數組中的元素是 declare-styleable 屬性集中的屬性的 ID,這樣就不需要自己再定義 int[] 了,數組中的每一個元素也會生成一個 ID 指向數組中的元素,ID 格式為:屬性集名_屬性名,直接通過屬性 ID 獲取對應 attr。
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomAttribute);
mColor = a.getColor(R.styleable.CustomAttribute_custom_color, mColor);
mRadius = a.getDimension(R.styleable.CustomAttribute_custom_radius, mRadius);
a.recycle();
也可以獲取系統(tǒng)中定義的屬性,這里 android:color
變成了 android_color
:
//把系統(tǒng)定義的屬性放在屬性集中
<declare-styleable name="CustomAttribute">
<attr name="android:color"/>
</declare-styleable>
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomAttribute);
mColor = a.getColor(R.styleable.CustomAttribute_android_color, mColor);
a.recycle();
Context#obtainStyledAttributes
上面獲取屬性的方式是調用了 Context 類的 obtainStyledAttributes 方法,該方法有四個重載方法:
//從 Theme 中獲取屬性
obtainStyledAttributes(int[] attrs)
//從 style 中獲取屬性
obtainStyledAttributes(int resid, int[] attrs)
//從 layout 文件中獲取屬性
obtainStyledAttributes(AttributeSet set, int[] attrs)
//從 layout 文件中獲取屬性
obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr,int defStyleRes)
參數中的 int[] attrs 就是要獲取的屬性的名稱,前面說過這個參數一般為 declare-styleable 在 R 文件的 styleable 類中生成的 ID。
obtainStyledAttributes(int[] attrs)
一個參數的方法是從應用的主題中獲取屬性,如果想從主題中獲取我們自定義的屬性,就需要在應用的主題中聲明自定義的屬性值。
加入定義了如下屬性:
<declare-styleable name="CustomAttribute">
<attr name="custom_color" format="color"/>
<attr name="custom_radius" format="dimension"/>
</declare-styleable>
那么在應用的主題中:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--直接在主題中指定-->
<item name="custom_color">#FF0000</item>
<item name="custom_radius">100dp</item>
</style>
obtainStyledAttributes(int resid, int[] attrs)
該方法是從指定的 style 中獲取,參數 resid 就是 style 在 R 文件中生成的 ID,我們需要在定義的 style 中聲明相應的屬性值:
<style name="CustomTheme">
<item name="custom_color">#00FF00</item>
<item name="custom_radius">10dp</item>
</style>
obtainStyledAttributes(AttributeSet set, int[] attrs)
該方法是最常用的,是從 layout 文件中獲取,AttributeSet 類型的參數是從 xml 文件中解析出來的控件屬性集合,也包括控件應用的 style 中聲明的屬性。
obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr,int defStyleRes)
第一個參數是從 xml 文件中解析出來的控件屬性集合,第三個參數是 style.xml 文件中定義的某一 Theme 的 ID,第四個參數是 style.xml 中定義的某一 Style 的 ID。
該方法首先從第一個參數 AttributeSet 也就是 layout 文件中獲取屬性,如果沒有取到屬性,那么從第三個參數 defStyleAttr 指定的主題中獲取,如果還沒有取到,那么從第四個參數 defStyleRes 指定的 style 中獲取,如果還沒有獲取到,那么返回的結果就為 null 了。
TypedArray
Context 類的 obtainStyledAttributes 方法實質調用的都是 Resources 類的內部類 Theme 類的對應的 obtainStyledAttributes 方法,最終調用的都是 ResourcesImpl.ThemeImpl 類的 obtainStyledAttributes 方法,這里不作討論,該方法返回的是一個 TypedArray 對象,TypedArray 類就是一個獲取到的屬性值數組的容器,該類提供了一系列獲取屬性值的方法:
//獲取屬性的數量
public int getIndexCount()
//獲取屬性名
public int getIndex (int at)
//獲取屬性類型
public int getType (int index)
//在使用之后要進行回收才能重用
public void recycle ()
public boolean getBoolean (int index, boolean defValue)
public int getColor (int index, int defValue)
publicColorStateList getColorStateList(int index)
public float getDimension (int index, float defValue)
public int getDimensionPixelOffset (int index, int defValue)
public int getDimensionPixelSize (int index, int defValue)
publicDrawable getDrawable(int index)
public float getFloat (int index, float defValue)
public float getFraction (int index, int base, int pbase, float defValue)
public int getInt (int index, int defValue)
public int getInteger (int index, int defValue)
public intgetLayoutDimension(int index,Stringname)
public int getResourceId (int index, int defValue)
publicString (int index)
publicCharSequencegetText(int index)
publicCharSequence[]getTextArray(int index)