如何以编程方式为视图分配 ID?
在 XML 文件中,我们可以为视图分配一个 ID,例如 android:id="@+id/something"
,然后调用 findViewById()
,但是在创建视图时以编程方式查看,如何分配 ID?
我认为 setId()
与默认分配不同。 setId()
是额外的。
有人可以纠正我吗?
In an XML file, we can assign an ID to a view like android:id="@+id/something"
and then call findViewById()
, but when creating a view programmatically, how do I assign an ID?
I think setId()
is not the same as default assignment. setId()
is extra.
Can anybody correct me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Android
id
概述Android
id
是一个常用于标识视图的整数;此id
可以通过 XML(如果可能)和代码(以编程方式)分配。id
对于获取 XML 定义的View
的引用最有用。由Inflater
生成的 code>(例如使用setContentView
。)通过
XML
分配id
android:id="@+id/
somename"
到您的视图。android:id
将被分配一个uniqueint
以在代码中使用。R.id.
somename”(实际上是一个常量)在代码中引用您的android:id
的int
值。int
可以在不同的版本中发生变化,因此永远不要从gen/
package.name/R.java 复制 id
,只需使用“
R.id.
somename”。Preference
生成其View
时,不会使用在 XML 中分配给Preference
的id
。)通过代码分配
id
(以编程方式)someView.setId(
int);
手动设置id
>int 必须是正数,但否则是任意的 - 它可以是无论你想要什么(如果这很可怕,请继续阅读。)id
的唯一性id
是唯一的。id
不必 必须是唯一的id
可以(理论上)与XML
-分配的id
。id
并不重要(继续阅读)。何时(以及为什么)冲突的
id
并不重要findViewById(int)
将从您指定的视图中以深度优先递归方式遍历视图层次结构 em> 并返回它找到的第一个具有匹配id
的View
。id
之前没有分配代码分配的id
,findViewById(R.id.somename)
将始终返回 XML 定义的视图,因此id
'd。动态创建视图并分配
ID
id
定义一个空的ViewGroup
。android:id="@+id/placeholder"
的LinearLayout
。ViewGroup 与
View
。id
。使用 placeholder.findViewById(convenientInt) 查询这些子视图;
API 17 引入了
View.generateViewId()
,它允许您生成唯一的 ID。如果您选择保留对视图的引用,请务必使用
getApplicationContext()
实例化它们,并确保在onDestroy
中将每个引用设置为 null代码>.显然泄露Activity
(在它被销毁后挂在它上面)是浪费的..:)保留一个XML
android:id
以在代码中使用API 17 引入了
View.generateViewId()
它生成一个唯一的 ID。(感谢 take-chances-make-changes 指出了这一点。 )*如果你的
ViewGroup
无法通过 XML 定义(或者您不希望如此),您可以通过 XML 保留 id 以确保其保持唯一:这里,values/ids.xml 定义自定义
id
:然后,一旦创建了 ViewGroup 或 View,您就可以附加自定义 id
冲突的
id example
为了清楚起见,通过混淆示例的方式,让我们检查一下当幕后发生
id
冲突时会发生什么。layout/mylayout.xml
为了模拟冲突,假设我们最新的构建分配了
R.id.placeholder
(@+id/placeholder
)int
值为12
..)定义了一些添加视图:
接下来,MyActivity.java 以编程方式(通过代码
placeholder
和我们的新产品之一TextView
的id
均为 12!但如果我们查询占位符的子视图,这并不是真正的问题:*还不错
Android
id
overviewAn Android
id
is an integer commonly used to identify views; thisid
can be assigned via XML (when possible) and via code (programmatically.) Theid
is most useful for getting references for XML-definedView
s generated by anInflater
(such as by usingsetContentView
.)Assign
id
viaXML
android:id="@+id/
somename"
to your view.android:id
will be assigned a uniqueint
for use in code.android:id
'sint
value in code using "R.id.
somename" (effectively a constant.)int
can change from build to build so never copy an id fromgen/
package.name/R.java
, just use "R.id.
somename".id
assigned to aPreference
in XML is not used when thePreference
generates itsView
.)Assign
id
via code (programmatically)id
s usingsomeView.setId(
int);
int
must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.)Uniqueness of
id
sXML
-assignedid
s will be unique.id
s do not have to be uniqueid
s can (theoretically) conflict withXML
-assignedid
s.id
s won't matter if queried correctly (keep reading).When (and why) conflicting
id
s don't matterfindViewById(int)
will iterate depth-first recursively through the view hierarchy from the View you specify and return the firstView
it finds with a matchingid
.id
s assigned before an XML-definedid
in the hierarchy,findViewById(R.id.somename)
will always return the XML-defined View soid
'd.Dynamically Creating Views and Assigning
ID
sViewGroup
withid
.LinearLayout
withandroid:id="@+id/placeholder"
.ViewGroup
withView
s.id
s that are convenient to each view.Query these child views using placeholder.findViewById(convenientInt);
API 17 introduced
View.generateViewId()
which allows you to generate a unique ID.If you choose to keep references to your views around, be sure to instantiate them with
getApplicationContext()
and be sure to set each reference to null inonDestroy
. Apparently leaking theActivity
(hanging onto it after is is destroyed) is wasteful.. :)Reserve an XML
android:id
for use in codeAPI 17 introduced
View.generateViewId()
which generates a unique ID. (Thanks to take-chances-make-changes for pointing this out.)*If your
ViewGroup
cannot be defined via XML (or you don't want it to be) you can reserve the id via XML to ensure it remains unique:Here, values/ids.xml defines a custom
id
:Then once the ViewGroup or View has been created, you can attach the custom id
Conflicting
id
exampleFor clarity by way of obfuscating example, lets examine what happens when there is an
id
conflict behind the scenes.layout/mylayout.xml
To simulate a conflict, lets say our latest build assigned
R.id.placeholder
(@+id/placeholder
) anint
value of12
..Next, MyActivity.java defines some adds views programmatically (via code):
So
placeholder
and one of our newTextView
s both have anid
of 12! But this isn't really a problem if we query placeholder's child views:*Not so bad
您只需使用
View.setId(integer)
即可。在 XML 中,即使您设置的是字符串 id,它也会转换为整数。因此,您可以使用任何(正)整数作为以编程方式添加的视图
。归功于这个答案。
You can just use the
View.setId(integer)
for this. In the XML, even though you're setting a String id, this gets converted into an integer. Due to this, you can use any (positive) Integer for theViews
you add programmatically.Credits to this answer.
是的,您可以在任何视图中使用您喜欢的任何(正)整数值调用
setId(value)
,然后使用findViewById(value)
在父容器中找到它。请注意,对于不同同级视图使用相同的值调用setId()
是有效的,但findViewById()
将仅返回第一个视图。Yes, you can call
setId(value)
in any view with any (positive) integer value that you like and then find it in the parent container usingfindViewById(value)
. Note that it is valid to callsetId()
with the same value for different sibling views, butfindViewById()
will return only the first one.