RelativeLayout中的动态视图(Dynamic Views in a RelativeLayout)

编程入门 行业动态 更新时间:2024-10-21 20:45:47
RelativeLayout中的动态视图(Dynamic Views in a RelativeLayout)

这个问题更多的是关于最佳实践。

我正在研究一些从动态XML文件创建UI的代码。 我花了很长时间才发现动态视图的ID为-1。 这意味着如果要将动态视图与其他动态视图相关联,则需要在创建时为其提供ID。

所以我这样做了,但我不太喜欢我提出的解决方案,而且我有可能不小心给两个元素提供相同的ID。

有没有办法让Android为这些动态视图分配唯一的ID,或者我是否必须自己创建某种ID跟踪代码?

This question is more about best practices.

I'm working on some code that creates a UI from a dynamic XML file. It took me quite a while to discover that Dynamic Views have an ID of -1. Which means if you want to layout Dynamic Views in relation to other Dynamic Views you need to give them IDs on creation.

So I did that, but I don't really like the solution I came up with, and there's a chance that I could accidentally give the same ID to two elements.

Is there a way to let Android assign unique IDs to these dynamic views, or do I have to create some sort of ID tracking code myself?

最满意答案

从这里开始

从API级别17开始,您可以致电

View.generateViewId()

Taken from here

From API level 17, you can call

View.generateViewId()

更多推荐

本文发布于:2023-04-28 04:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   动态   RelativeLayout   Dynamic   Views

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!