大家好,我是程序員雙木L,後續會發專題類的文章,這是自定義控件的第一篇,之後也會陸續更新相關的文章,歡迎關注。
自定義屬性在自定義控件過程中屬於比較常見的操作,我們可以回想一下這樣的場景:自定義view的過程中,我們需要在不同的情況下設置不同的文字大小,那麼我們是不是就需要提供對外的方法來設置,這樣就比較靈活操作。而我們自定義對外的方法,就是我們自定義的屬性啦,那我們來分析一下其原理及作用。
下面我們根據例子來進行分析:
1、首先我們需要在res->values目錄下新建attrs.xml文件,該文件就是用來聲明屬性名及其接受的數據格式的,如下:
<?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那就會做像素轉換;
float:浮點值;
integer:整型值;
string:字符串;
fraction:百分數;
enum:枚舉值;
flag:是自己定義的,就是裏面對應了自己的屬性值;
reference:指向其它資源;
reference|color:顏色的資源文件;
reference|boolean:布爾值的資源文件.
2、自定義屬性的使用,這裏我們使用兩種方式進行對比解析
最最最原始的使用方式
(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方法的參數AttributeSet是個xml解析工具類,幫助我們從佈局的xml裏提取屬性名和屬性值。
(2)、在佈局文件xml中的使用
<?xml version="1.0" encoding="utf-8"?>
<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.TestAttrsView
android: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”.冒號後面是給這個引用起的別名。
schemas是xml文檔的兩種約束文件其中的一種,規定了xml中有哪些元素(標籤)、
元素有哪些屬性及各元素的關係,當然從面向對象的角度理解schemas文件可以
認爲它是被約束的xml文檔的“類”或稱爲“模板”。
(3)、將屬性名與屬性值打印結果如下:
從打印結果我們可以看出,AttributeSet將佈局文件xml下的屬性全部打印出來了,細心的童鞋可能已經看出來:
xml文件:
app:view_ref="@dimen/dp_15"
打印結果:
name:view_ref value:@2131034213
這個屬性我們設置的是一個整數尺寸,可最後打印出來的是資源編號。
那如果我們想要輸出我們設置的整數尺寸,需要怎麼操作呢?
這個時候就該我們這篇的主角出場了,使用TypedArray方式。
- 使用TypedArray方式
(1)、這裏我們需要將attrs.xml使用“declare-styleable”標籤進行改造,如下:
<?xml version="1.0" encoding="utf-8"?>
<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文件可以看出,我們將屬性聲明歸結到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();
}
}
這裏我直接打印出解析結果,這裏可以獲取我們想要的自定義屬性,而系統有的屬性可以忽略。
(3)、運行結果如下
從解析的結果可以看出,尺寸的結果已經轉換爲實際值了:
xml文件:
app:view_ref="@dimen/dp_15"
打印結果:
name:view_ref value:41.25
這個時候有童鞋又問了,我設置的是15dp,爲啥最後打印是41.25了呢?其實解析出來的值單位是px,所以這裏輸出的是轉換後的值。
解析的過程中用到了這個方法:
context.obtainStyledAttributes(attrs, R.styleable.TestStyleable);
我們來看一下這個方法的源碼:
public final TypedArray obtainStyledAttributes(
@Nullable AttributeSet set, @NonNull @StyleableRes int[] attrs) {
return getTheme().obtainStyledAttributes(set, attrs, 0, 0);
}
源碼中我們可以看到這個方法有兩個參數:
AttributeSet set:表示當前xml聲明的屬性集合
int[] attrs:表示你想挑選的屬性,你想得到哪些屬性,你就可以將其寫到這個int數組中
obtainStyledAttributes方法返回值類型爲TypedArray。該類型記錄了獲取到的屬性值集合,而通過數組下標索引即可找到對應的屬性值。索引下標通過R.styleable.TestStyleable_xx獲取,"xx"表示屬性名,一般命名爲"styleable名" + "_" + "屬性名"。
而TypedArray提供了各種Api,如getInteger,getString,getDimension等方法來獲取屬性值,這些方法都需要傳入對應屬性名在obtainStyledAttributes中的int數組的位置索引,通過下標獲取數組裏屬性值。
這個TypedArray的作用就是資源的映射作用,把自定義屬性在xml設置值映射到class,這樣怎麼獲取都很簡單啦。
到這裏就分析完啦!