1.組件的屬性
自定義的組件繼承自View後就會具備若干的默認屬性。
除了View 的默認屬性之外、我們也可以爲組件自定義屬性,自定義屬性應遵循以下步驟:
1)在 res/values/attrs.xml 文件中爲指定組件定義 declare-styleable 標記, 並將所有的屬性
都定義在該標記中;(若沒有,自行創建)
2)在 layout 文件中使用自定義屬性;
3)在組件類的構造方法中讀取屬性值。example:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CirclePhotoView"> <attr name="text" format="string" /> </declare-styleable> </resources>
一般來講,定義好屬性的名稱和類型之後,屬性就可以使用了,但是在佈局文件中,要注意屬性的命名空間(namespace),默認的命名空間是“Android”,是由語句
xmlns:android="http://schemas.android.com/apk/res/android"
所決定的,但是對於自定義屬性,必須定義其他的命名空間,
xmlns:app="http://schemas.android.com/apk/res-auto"
其中,“app”是可以使用其他的進行替代,但是後面"http://schemas.android.com/apk/res-auto"則是固定的,當有了這個命名空間之後,之前的自定義屬性便可以對其進行使用了。當組件運行之後,所有的屬性都將保存在AttributeSet集合中並通過構造方法進行傳入,我們可以通過TypedArray讀取指定的屬性值。
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CirclePhotoView);
text = typedArray.getString(R.styleable.CirclePhotoView_text);
typedArray.recycle();
2.讀取來自style和theme中的屬性
關於組件的屬性,可以在四個地方進行定義:
1)組件中定義
2) 組件的style屬性中進行定義
3)theme中進行定義
4)theme的style的屬性中進行定義
關於四個地方獲取屬性的方法,首先我們在attr.xml中定義如下四個屬性:
<resources>
<declare-styleable name="CirclePhotoView">
<attr name="attr1" format="string" />
<attr name="attr2" format="string" />
<attr name="attr3" format="string" />
<attr name="attr4" format="string" />
</declare-styleable>
<attr name="myStyle" format="reference" />
</resources>
其中,“myStyle”類型爲reference,用於theme的style屬性。
下面四個屬性將應用於不同的場合,在佈局文件中:
<com.example.hu.customizationview.CirclePhotoView
android:id="@+id/myview"
style="@style/viewStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:attr1="你好啊 你是誰啊" />
在res/values/styles.xml中:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="attr3">3你說啥 我不知道</item>
<item name="myStyle">@style/myDefaultStyle</item>
</style>
<style name="myDefaultStyle">
<item name="attr4">這個是4</item>
</style>
<style name="viewStyle">
<item name="attr2">你真的不知道這個是2?</item>
</style>
</resources>
最後在CirclePhotoView對屬性值進行獲取:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePhotoView,
defStyleAttr, R.style.myDefaultStyle);
String attr1 = a.getString(R.styleable.CirclePhotoView_attr1);
String attr2 = a.getString(R.styleable.CirclePhotoView_attr2);
String attr3 = a.getString(R.styleable.CirclePhotoView_attr3);
String attr4 = a.getString(R.styleable.CirclePhotoView_attr4);
在上面的構造方法中,我們調用了
public TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)。在該方法中:
1)set:屬性值的集合。
2)attrs:我們要獲取的屬性的資源 ID 的一個數組, 我們定義了 attr1、 attr2、 attr3 和 attr4, 這
4 個屬性自動生成的索引會存儲到 R.styleable.AttrView 數組中, 該數組就是 attrs 參數。
3)defStyleAttr:當前 Theme 中 style 屬性, 如果組件和組件的 style 屬性都沒有爲 View 指定屬
性時,將從 Theme 的 Style 中查找相應的屬性值。
4)defStyleRes:指向一個 Style 的資源 ID,但是僅在 defStyleAttr 爲 0 或 defStyleAttr 不爲 0 但
Theme 中沒有爲 defStyleAttr 屬性賦值時起作用。
在下面的圖中展示了attr屬性的讀取順序: