getResourceId 在自定义控件中找不到 ID
我正在尝试创建一个控件(单击标题时展开和收缩的面板),并且我在网上找到了一些代码。在构造函数中,我
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyControl);
...
int headerId = array.getResourceId(R.styleable.MyControl_header, -1);
使用以下 XML 在布局文件中创建控件:
<MyControl
android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/drawer"
header="@+id/header" content="@+id/drawerContent"
android:layout_below="@id/contentContainer" android:background="#00FF00">
<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@id/header"
android:text="This is a header"/>
<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@id/drawerContent"
android:text="@string/sample_text" />
</MyControl>
问题是,getResourceId()
返回 -1 (即,它似乎找不到资源设置为属性)。
知道为什么吗?
编辑:忘记包含我的 attrs.xml 文件:
<resources>
<declare-styleable name="MyControl">
<attr name="collapsedHeight" format="dimension" />
<attr name="header" format="reference" />
<attr name="content" format="reference" />
<attr name="animationDuration" format="integer" />
</declare-styleable>
编辑2:不知何故,我没有想到要检查其他属性——我添加了一些其他属性。我也在调试器中检查了它们的值,看起来它们也默认了。因此,这不是 getResourceId
的问题,这与我获取属性的一般方式有关。我是 Android 新手,所以任何人都可以看到我的属性处理代码中的任何内容吗?
I'm trying to create a control (a panel that expands and contracts when a header is clicked), and I found some code online. In the constructor, I have
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyControl);
...
int headerId = array.getResourceId(R.styleable.MyControl_header, -1);
The control is being created in a layout file with the following XML:
<MyControl
android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/drawer"
header="@+id/header" content="@+id/drawerContent"
android:layout_below="@id/contentContainer" android:background="#00FF00">
<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@id/header"
android:text="This is a header"/>
<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@id/drawerContent"
android:text="@string/sample_text" />
</MyControl>
The problem is, getResourceId()
comes back with -1 (i.e., it can't seem to find the resource set to the attribute).
Any idea why?
EDIT: Forgot to include my attrs.xml file:
<resources>
<declare-styleable name="MyControl">
<attr name="collapsedHeight" format="dimension" />
<attr name="header" format="reference" />
<attr name="content" format="reference" />
<attr name="animationDuration" format="integer" />
</declare-styleable>
EDIT 2: Somehow, I didn't think to check the other attributes--there were a couple of other attributes I had added. I checked their values in the debugger, too, and it looks like they're defaulting, as well. So it's not an issue with getResourceId
, it's something to do with how I'm getting the attributes in general. I'm new to Android, so can anyone see anything in my attribute processing code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了。事实证明,属性必须在 XML 中命名;我添加了,
但需要
添加这些之后,它发现这些值很好。
Figured it out. It turns out, the attributes have to be namespaced in the XML; I put
but it needed to be
After I added those, it found the values just fine.
您是否在values->中添加了资源? attrs.xml 文件?
Did you add the resources in values-> attrs.xml file?