Android:以编程方式 View.setID(int id) - 如何避免 ID 冲突? [英] Android: View.setID(int id) programmatically - how to avoid ID conflicts?
问题描述
我在 for 循环中以编程方式添加 TextViews 并将它们添加到 ArrayList.
I'm adding TextViews programmatically in a for-loop and add them to an ArrayList.
如何使用 TextView.setId(int id)
?我想出什么整数 ID 才能与其他 ID 不冲突?
How do I use TextView.setId(int id)
? What Integer ID do I come up with so it doesn't conflict with other IDs?
推荐答案
根据查看
文档
标识符在此视图的层次结构中不必是唯一的.标识符应为正数.
The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
所以你可以使用任何你喜欢的正整数,但在这种情况下,可能会有一些具有等效 id 的视图.如果你想在层次结构中搜索一些视图,调用 setTag
和一些关键对象可能会很方便.
So you can use any positive integer you like, but in this case there can be some views with equivalent id's. If you want to search for some view in hierarchy calling to setTag
with some key objects may be handy.
这篇关于Android:以编程方式 View.setID(int id) - 如何避免 ID 冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!