这个问题更多的是关于最佳实践。
我正在研究一些从动态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()更多推荐
发布评论