3
我已经定义了一个父styleable
一些自定义视图如下检索继承属性
<declare-styleable name="ParentView">
<attr name="color" format="color" />
<attr name="rotate" format="float" />
</declare-styleable>
我再定义一个孩子styleable
从父继承设置样式的属性,即
<declare-styleable name="ParentView.ChildView">
<attr name="state">
<enum name="state0" value="0"/>
<enum name="state1" value="1"/>
</attr>
</declare-styleable>
现在,我可以在我的自定义视图检索孩子设置样式属性值,而不是从其父设置样式,即任何属性设置为XML我的自定义视图
<com.example.android.MyCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:color="@color/orange"
custom:state="state1" />
,并在我的自定义视图的构造
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ParentView_ChildView, 0, 0);
try {
state = array.getInt(R.styleable.ParentView_ChildView_state, state);
color = array.getInt(R.styleable.ParentView_color, Color.WHITE);
}
finally {
array.recycle();
}
使用下面的代码检索到state
正确属性,但属性color
始终只是给它的默认值,即白。我在这里错过了什么吗?