我在我的应用程序中使用自定义View
,
我声明了这样的属性
<declare-styleable name="MySelectView">
<attr name="selected" format="boolean" />
<attr name="iconSize" format="dimension" />
<attr name="imageSelected" format="reference" />
<attr name="imageUnSelected" format="reference" />
<attr name="vectorImageSelected" format="reference" />
<attr name="vectorImageUnSelected" format="reference" />
<attr name="setOnClickListener" format="boolean" />
</declare-styleable>
在我的View
中,我获取的属性值如下所示。
private void applyAttributeSet(AttributeSet attrs) {
if (attrs == null) {
return;
}
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.MySelectView,
0, 0);
try {
selected = a.getBoolean(R.styleable.MySelectView_selected, false);
size = a.getDimension(R.styleable.MySelectView_iconSize, Util.dpToPx(size));
selectedDrawable = a.getDrawable(R.styleable.MySelectView_imageSelected);
unSelectedDrawable = a.getDrawable(R.styleable.MySelectView_imageUnSelected);
setOnClickListener = a.getBoolean(R.styleable.MySelectView_setOnClickListener, true);
} finally {
a.recycle();
}
}
<com.mathrusoft.infogo.widget.MySelectView
android:id="@+id/favorite"
android:layout_width="@dimen/news_item_bottom_view_size"
android:layout_height="@dimen/news_item_bottom_view_size"
android:layout_marginLeft="20dp"
app:iconSize="@dimen/news_item_bottom_view_size"
app:imageSelected="@drawable/ic_star_24dp"
app:imageUnSelected="@drawable/ic_star_border_black_24dp"
app:setOnClickListener="false" />
如果我在app:imageSelected
或app:imageUnSelected
发送正常的drawable,如果我发送了矢量图片,那么它可以正常工作,我在Exception
1}}中收到错误{/ 1}} p>
selectedDrawable = a.getDrawable(R.styleable.MySelectView_imageSelected);