View.setTag - 需要多个键:resourceIDs

发布于 2024-12-19 14:31:47 字数 275 浏览 2 评论 0原文

我正在创建一个自定义视图以包含在类库中。我需要向我动态创建的子视图(在代码中,而不是 xml 中)添加多个标签,因此这需要我添加 use:View.setTag(int key, Object tag)

密钥需要是项目 R 文件中定义的 id,但由于我的所有视图都是动态创建的,并且代码将进入库以供其他项目使用,所以我手头没有硬编码的资源 id 可供使用。有什么方法可以以编程方式发现几个已定义的资源 ID,或以编程方式定义一些资源 ID?或者我对这一切都错了?

谢谢

I'm creating a custom View to be included in a class library. I need to add multiple tags to subviews that I am creating dynamically (in code, not xml), so this requires that I add use: View.setTag(int key, Object tag).

The key needs to be an id defined in the project R file, but since all of my views are dynamically created and the code is going into a library for use in other projects, I don't have a hardcoded resource id on hand to use. Is there any way to discover a couple of defined resource ids programatically, or to define some programatically? Or am I going about this all wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦幻的味道 2024-12-26 14:31:47

请记住,您可以添加任何对象作为标签。如果要添加到视图中的数据是动态的,那么最简单的方法是将哈希表作为标签添加到视图中。然后将所需的所有键/值对添加到该哈希表中。另一方面,(处理标签的代码),您可以通过将视图的标签转换回哈希表来迭代值,然后迭代 Hashtable.keys()

Keep in mind that you can add any object as a tag. If the data you're adding to the view is dynamic, then the easiest thing to do would be to add a Hashtable to the View as the tag. Then add all the key/value pairs you want to that Hashtable. On the other end, (the code processing the tag), you can iterate over the values by casting the View's tag back into a Hashtable, and iterating over Hashtable.keys().

戏蝶舞 2024-12-26 14:31:47

如果您阅读文档< /a>,它说:

标签还可以用于在视图中存储数据,而无需求助于其他数据结构。

暗示您可以做的就是使用另一种数据结构。你可能想看看
在使用 ViewHolder 模式并将其实例存储在标签中。

If you read the documentation, it says:

Tags can also be used to store data within a view without resorting to another data structure.

Implying that something you could do is to use another data structure. You might want to look
at using the ViewHolder pattern and storing an instance of that in the tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文