getResourceId 在自定义控件中找不到 ID

发布于 2024-12-02 04:18:37 字数 1537 浏览 0 评论 0原文

我正在尝试创建一个控件(单击标题时展开和收缩的面板),并且我在网上找到了一些代码。在构造函数中,我

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椒妓 2024-12-09 04:18:37

想通了。事实证明,属性必须在 XML 中命名;我添加了,

<MyControl
    android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/drawer"
    header="@+id/header" content="@+id/drawerContent"...

但需要

<MyControl xmlns:myPackage="http://schemas.android.com/apk/res/com.my.package"
    android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/drawer"
    myPackage:header="@+id/header" myPackage:content="@+id/drawerContent"

添加这些之后,它发现这些值很好。

Figured it out. It turns out, the attributes have to be namespaced in the XML; I put

<MyControl
    android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/drawer"
    header="@+id/header" content="@+id/drawerContent"...

but it needed to be

<MyControl xmlns:myPackage="http://schemas.android.com/apk/res/com.my.package"
    android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/drawer"
    myPackage:header="@+id/header" myPackage:content="@+id/drawerContent"

After I added those, it found the values just fine.

情绪 2024-12-09 04:18:37

您是否在values->中添加了资源? attrs.xml 文件?

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyControl">
    <attr name="headerId" format="integer" />
    </declare-styleable>
</resources>

Did you add the resources in values-> attrs.xml file?

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyControl">
    <attr name="headerId" format="integer" />
    </declare-styleable>
</resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文