【发布时间】:2014-06-25 13:22:57
【问题描述】:
我正在尝试将自定义属性 (@drawable) 添加到我的自定义视图中,并且我想将其作为 resourceId 进行检索。如果我想通过代码访问它,我的视图以某种方式找不到它:
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TouchImageView, defStyle, 0);
int base = a.getResourceId(R.styleable.imageDrawable, R.drawable.kaart);
我得到的错误是: 无法解析或不是字段
我的 attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TouchImageView">
<attr name="imageDrawable" format="string"/>
</declare-styleable>
</resources>
布局中的 xml 将是:
<nl.raakict.android.util.TouchImageView
android:id="@+id/plattegrond"
android:layout_width="match_parent"
android:layout_height="match_parent"
imageDrawable="@drawable/kaart"/>
【问题讨论】:
-
你需要在你的
TouchImageView classimport yourPKG.R; -
是的,我这样做了。它确实识别 R 但不识别 R.styleable.imageDrawable
标签: android xml android-custom-view declare-styleable