在自定義的View中,很多時候我們需要對View添加自定義屬性步驟如下:
1) 編寫xml文件,定義屬性名稱與屬性數據類型
//attrs.xml
<resources>
<declare-styleable name="MyView">
<attr name="paint_color" format="integer"/>
<attr name="paint_width" format="integer"/>
</declare-styleable>
</resources>
2) 從Layout種解析並獲得屬性值
//MyTestView.java
public MyTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTestView);
mPaintColor = typedArray.getInt(R.styleable.MyTestView_paint_color, Color.RED);
mPaintWidth = typedArray.getInt(R.styleable.MyTestView_paint_width, 2);
}
obtainStyledAttributes的進一步學習
通過進一步的閱讀可以知道Context.obtainStyleAttributes函數被重載了多次,但最終都會調用Theme.obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes)函數。首先,觀察View的構造函數:
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies default values for
* the view. Can be 0 to not look for defaults.
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the view, used only if
* defStyleAttr is 0 or can not be found in the theme. Can be 0
* to not look for defaults.
* @see #View(Context, AttributeSet, int)
*/
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)
其中:
- set表示佈局xml文件中爲View添加的屬性集合
- defStyleAttr表示在Theme中定義的屬性,如果爲0則表示加載默認Theme
- defStyleRes表示從Resource中直接讀取某個樣式
所以當需要通過Theme或Style來控制自定義View的屬性時需要指定構造函數中的對應參數並調用。
public MyTestView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.custom_style);
}
public MyTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, R.style.myStyle);
}
如何定義style,或在Theme中添加屬性
自定義style
指定style很好理解,在styles.xml中定義自己的style即可
//styles.xml
<style name="myStyle">
<item name="android:textColor">@color/colorPrimaryDark</item>
</style>
在Theme中添加屬性
- 首先在xml文件中聲明屬性名custom_style與類型,由於custom_style用於Theme中,所以不放在<declare_styleable/>中:
//attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTestView">
<attr name="paint_color" format="integer"/>
<attr name="paint_width" format="integer"/>
</declare-styleable>
<attr name="custom_style" format="reference"/>
</resources>
- 然後在被使用的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="custom_style">@style/customStyle</item>
</style>
<style name="customStyle">
<item name="android:textColor">@color/colorAccent</item>
</style>
所以單個屬性可以通過Layout、Theme、Style來控制,但三個途徑的值不可能同時有效,他們的優先級如下:
set(Layout) > defStyleAttr(Theme) > defStyleRes(Style) > NULL (主題中直接指定)
所以當Context.obtainStyleAttributes調用是,將set設置爲null則可以獲得Theme中的值,同理,將前兩者分別設爲null和0,則可以獲取Style中的值。