在自定义的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中的值。