自定義View之自定屬性
阿新 • • 發佈:2018-12-10
在自定義的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中的值。