Android自定義屬性TypedArray詳解
大家好,我是程序員雙木L,后續(xù)會(huì)發(fā)專題類的文章,這是自定義控件的第一篇,之后也會(huì)陸續(xù)更新相關(guān)的文章,歡迎關(guān)注。
自定義屬性在自定義控件過程中屬于比較常見的操作,我們可以回想一下這樣的場景:自定義view的過程中,我們需要在不同的情況下設(shè)置不同的文字大小,那么我們是不是就需要提供對外的方法來設(shè)置,這樣就比較靈活操作。而我們自定義對外的方法,就是我們自定義的屬性啦,那我們來分析一下其原理及作用。
下面我們根據(jù)例子來進(jìn)行分析:
1、首先我們需要在res->values目錄下新建attrs.xml文件,該文件就是用來聲明屬性名及其接受的數(shù)據(jù)格式的,如下:
<?xml version="1.0" encoding="utf-8"?><resources><attr name="view_int" format="integer" /><attr name="view_str" format="string" /><attr name="view_bool" format="boolean" /><attr name="view_color" format="color" /><attr name="view_ref" format="reference" /><attr name="view_float" format="float" /><attr name="view_dim" format="dimension" /><attr name="view_frac" format="fraction" /><attr name="view_enum"><enum name="num_one" value="1" /><enum name="num_two" value="2" /><enum name="num_three" value="3" /><enum name="num_four" value="4" /></attr><attr name="view_flag"><flag name="top" value="0x1" /><flag name="left" value="0x2" /><flag name="right" value="0x3" /><flag name="bottom" value="0x4" /></attr></resources>
attr名詞解析:
name表示屬性名,上面的屬性名是我自己定義的。
format表示接受的輸入格式,format格式集合如下:
color:顏色值;boolean:布爾值;dimension:尺寸值,注意,這里如果是dp那就會(huì)做像素轉(zhuǎn)換;float:浮點(diǎn)值;integer:整型值;string:字符串;fraction:百分?jǐn)?shù);enum:枚舉值;flag:是自己定義的,就是里面對應(yīng)了自己的屬性值;reference:指向其它資源;reference|color:顏色的資源文件;reference|boolean:布爾值的資源文件.
2、自定義屬性的使用,這里我們使用兩種方式進(jìn)行對比解析
最最最原始的使用方式
(1)、自定義文件如下:
public class TestAttrsView extends View {private final String TAG = "TestAttrsView:";public TestAttrsView(Context context) {this(context, null);}public TestAttrsView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public TestAttrsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);//最原始使用方式for (int i = 0; i < attrs.getAttributeCount(); i++) {Log.i(TAG, "name:" + attrs.getAttributeName(i) + " value:" + attrs.getAttributeValue(i));}}}
我們可以在TestAttrsView方法的參數(shù)AttributeSet是個(gè)xml解析工具類,幫助我們從布局的xml里提取屬性名和屬性值。
(2)、在布局文件xml中的使用
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><com.example.viewdemo.customView.TestAttrsViewandroid:layout_width="200dp"android:layout_height="200dp"app:view_bool="true"app:view_color="#e5e5e5"app:view_dim="10px"app:view_float="5.0"app:view_frac="100%"app:view_int="10"app:view_ref="@dimen/dp_15"app:view_str="test attrs view" /></FrameLayout>
這里使用自定義屬性需要聲明xml的命名空間,其中app是命名空間,用來加在自定義屬性前面。
xmlns:app="http://schemas.android.com/apk/res-auto"聲明xml命名空間,xmlns意思為“xml namespace”.冒號后面是給這個(gè)引用起的別名。schemas是xml文檔的兩種約束文件其中的一種,規(guī)定了xml中有哪些元素(標(biāo)簽)、元素有哪些屬性及各元素的關(guān)系,當(dāng)然從面向?qū)ο蟮慕嵌壤斫鈙chemas文件可以認(rèn)為它是被約束的xml文檔的“類”或稱為“模板”。
(3)、將屬性名與屬性值打印結(jié)果如下:

從打印結(jié)果我們可以看出,AttributeSet將布局文件xml下的屬性全部打印出來了,細(xì)心的童鞋可能已經(jīng)看出來:
xml文件:app:view_ref="@dimen/dp_15"打印結(jié)果:name:view_ref value:@2131034213
這個(gè)屬性我們設(shè)置的是一個(gè)整數(shù)尺寸,可最后打印出來的是資源編號。
那如果我們想要輸出我們設(shè)置的整數(shù)尺寸,需要怎么操作呢?
這個(gè)時(shí)候就該我們這篇的主角出場了,使用TypedArray方式。
使用TypedArray方式
(1)、這里我們需要將attrs.xml使用“declare-styleable”標(biāo)簽進(jìn)行改造,如下:
<resources><declare-styleable name="TestStyleable"><attr name="view_int" format="integer" /><attr name="view_str" format="string" /><attr name="view_bool" format="boolean" /><attr name="view_color" format="color" /><attr name="view_ref" format="reference" /><attr name="view_float" format="float" /><attr name="view_dim" format="dimension" /><attr name="view_frac" format="fraction" /><attr name="view_enum"><enum name="num_one" value="1" /><enum name="num_two" value="2" /><enum name="num_three" value="3" /><enum name="num_four" value="4" /></attr><attr name="view_flag"><flag name="top" value="0x1" /><flag name="left" value="0x2" /><flag name="right" value="0x3" /><flag name="bottom" value="0x4" /></attr></declare-styleable></resources>
從改造后的attrs文件可以看出,我們將屬性聲明歸結(jié)到TestStyleable里面,也就意味著這些屬性是屬于TestStyleable下的。
(2)、屬性的解析:
public class TestAttrsView extends View {private final String TAG = "TestAttrsView:";public TestAttrsView(Context context) {this(context, null);}public TestAttrsView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public TestAttrsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);//最原始使用方式/* for (int i = 0; i < attrs.getAttributeCount(); i++) {Log.i(TAG, "name:" + attrs.getAttributeName(i) + " value:" + attrs.getAttributeValue(i));}*///使用TypeArray方式//R.styleable.TestStyleable 指的是想要解析的屬性TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TestStyleable);int integerView = typedArray.getInt(R.styleable.TestStyleable_view_int, 0);Log.i(TAG, "name:view_int" + " value:" + integerView);boolean aBooleanView = typedArray.getBoolean(R.styleable.TestStyleable_view_bool, false);Log.i(TAG, "name:view_bool" + " value:" + aBooleanView);int colorView = typedArray.getColor(R.styleable.TestStyleable_view_color, Color.WHITE);Log.i(TAG, "name:view_color" + " value:" + colorView);String stringView = typedArray.getString(R.styleable.TestStyleable_view_str);Log.i(TAG, "name:view_str" + " value:" + stringView);float refView = typedArray.getDimension(R.styleable.TestStyleable_view_ref, 0);Log.i(TAG, "name:view_ref" + " value:" + refView);float aFloatView = typedArray.getFloat(R.styleable.TestStyleable_view_float, 0);Log.i(TAG, "name:view_float" + " value:" + aFloatView);float dimensionView = typedArray.getDimension(R.styleable.TestStyleable_view_dim, 0);Log.i(TAG, "name:view_dim" + " value:" + dimensionView);float fractionView = typedArray.getFraction(R.styleable.TestStyleable_view_frac, 1, 1, 0);Log.i(TAG, "name:view_frac" + " value:" + fractionView);//typedArray存放在緩存池,使用完需要釋放緩存池typedArray.recycle();}}
這里我直接打印出解析結(jié)果,這里可以獲取我們想要的自定義屬性,而系統(tǒng)有的屬性可以忽略。
(3)、運(yùn)行結(jié)果如下

從解析的結(jié)果可以看出,尺寸的結(jié)果已經(jīng)轉(zhuǎn)換為實(shí)際值了:
xml文件:app:view_ref="@dimen/dp_15"打印結(jié)果:name:view_ref value:41.25
這個(gè)時(shí)候有童鞋又問了,我設(shè)置的是15dp,為啥最后打印是41.25了呢?其實(shí)解析出來的值單位是px,所以這里輸出的是轉(zhuǎn)換后的值。
解析的過程中用到了這個(gè)方法:
context.obtainStyledAttributes(attrs, R.styleable.TestStyleable);我們來看一下這個(gè)方法的源碼:
public final TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs) {return getTheme().obtainStyledAttributes(set, attrs, 0, 0);}
源碼中我們可以看到這個(gè)方法有兩個(gè)參數(shù):
AttributeSet set:表示當(dāng)前xml聲明的屬性集合int[] attrs:表示你想挑選的屬性,你想得到哪些屬性,你就可以將其寫到這個(gè)int數(shù)組中
obtainStyledAttributes方法返回值類型為TypedArray。該類型記錄了獲取到的屬性值集合,而通過數(shù)組下標(biāo)索引即可找到對應(yīng)的屬性值。索引下標(biāo)通過R.styleable.TestStyleable_xx獲取,"xx"表示屬性名,一般命名為"styleable名" + "_" + "屬性名"。
而TypedArray提供了各種Api,如getInteger,getString,getDimension等方法來獲取屬性值,這些方法都需要傳入對應(yīng)屬性名在obtainStyledAttributes中的int數(shù)組的位置索引,通過下標(biāo)獲取數(shù)組里屬性值。
這個(gè)TypedArray的作用就是資源的映射作用,把自定義屬性在xml設(shè)置值映射到class,這樣怎么獲取都很簡單啦。
到這里就分析完啦!
