更新时间:2023-01-19 19:04:38
这是Android的 ID
是一个整数,通常用来确定意见;这个 ID
可以通过XML被分配(如果可能),并通过code(编程)的 ID
是获取用于XML定义的查看
S由一个充气
(如生成使用的setContentView )
id
overviewAn Android id
is an integer commonly used to identify views; this id
can be assigned via XML (when possible) and via code (programmatically.) The id
is most useful for getting references for XML-defined View
s generated by an Inflater
(such as by using setContentView
.)
Android的一个属性:ID =@ + ID /
somename
到视图。 LI>
机器人:ID
将被分配一个唯一 INT
在code使用。机器人:ID
的 INT
用,在code值 R.id。
somename(实际上是一个常数。) INT
可以从构建更改建的那么从来没有从复制一个id
GEN / package.name / R.java
,只要使用 R.id。
somename。 ID
在XML中分配给 preference
不使用时, preference
生成的查看
)android:id="@+id/
somename"
to your view.android:id
will be assigned a unique int
for use in code.android:id
's int
value in code using "R.id.
somename" (effectively a constant.)int
can change from build to build so never copy an id from gen/
package.name/R.java
, just use "R.id.
somename".id
assigned to a Preference
in XML is not used when the Preference
generates its View
.) ID
s,使用 someView.setId(
INT );
INT
必须是积极的,但另有arbitrary-它可以是任何你想要的(请继续阅读,如果这是可怕的。)id
s using someView.setId(
int);
int
must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.) XML
-assigned ID
旨意是唯一的。 ID
■不要的没有的必须是唯一 ID
S能(理论上)冲突 XML
-assigned ID
秒。 ID
旨意并不重要,如果查询正确的(请继续阅读)的XML
-assigned id
s will be unique.id
s do not have to be uniqueid
s can (theoretically) conflict with XML
-assigned id
s.id
s won't matter if queried correctly (keep reading). findViewById(INT)
将遍历深度优先递归通过视图层次结构的从查看指定的,返回的第一个查看
它匹配查找 ID
。 ID
秒的XML定义的 ID之前分配
在层次结构中, findViewById(R.id.somename)
将始终返回XML定义的视图,使 ID
'D findViewById(int)
will iterate depth-first recursively through the view hierarchy from the View you specify and return the first View
it finds with a matching id
.id
s assigned before an XML-defined id
in the hierarchy, findViewById(R.id.somename)
will always return the XML-defined View so id
'd.的ViewGroup
与 ID
。的LinearLayout
与机器人:ID =@ + ID /占位符
的ViewGroup
与查看
秒。 ID
s表示是方便每个视图。查询中使用placeholder.findViewById(convenientInt)这些孩子的意见;
ViewGroup
with id
.LinearLayout
with android:id="@+id/placeholder"
.ViewGroup
with View
s.id
s that are convenient to each view.Query these child views using placeholder.findViewById(convenientInt);
17 API介绍 View.generateViewId()
,它允许您生成一个唯一的ID。
API 17 introduced View.generateViewId()
which allows you to generate a unique ID.
如果您选择保留参考大家的意见左右,一定要与实例化它们getApplicationContext()
,并务必将每一个参考为NULL 的onDestroy
。显然的泄漏的的活动
(挂到后就是被破坏),是一种浪费。。:)
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 in onDestroy
. Apparently leaking the Activity
(hanging onto it after is is destroyed) is wasteful.. :)
17 API介绍的 View.generateViewId()
的生成一个唯一的ID。的(感谢通吃机会-make-变化指出这点。)*
API 17 introduced View.generateViewId()
which generates a unique ID. (Thanks to take-chances-make-changes for pointing this out.)*
如果你的的ViewGroup
无法通过XML来定义(或者你不希望它),你可以通过XML保留的ID,以确保它仍然是独一无二的:
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:
这里,值/ ids.xml 的定义自定义 ID
:
Here, values/ids.xml defines a custom id
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="reservedNamedId" type="id"/>
</resources>
然后,一旦的ViewGroup或查看已创建,您可以将自定义ID
myViewGroup.setId(R.id.reservedNamedId);
ID
例如有关清晰混淆通过举例的方式,让我们看会发生什么时,有一个 ID幕后
冲突。
id
exampleFor clarity by way of obfuscating example, lets examine what happens when there is an id
conflict behind the scenes.
布局/ mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
应用于模拟冲突,可以说我们的最新版本分配 R.id.placeholder
( @ + ID /占位符
)的 INT
值的 12
..
其次, MyActivity.java 定义了一些补充意见编程方式(通过code):
Next, MyActivity.java defines some adds views programmatically (via code):
int placeholderId = R.id.placeholder; // placeholderId==12
// returns *placeholder* which has id==12:
ViewGroup placeholder = (ViewGroup)this.findViewById(placeholderId);
for (int i=0; i<20; i++){
TextView tv = new TextView(this.getApplicationContext());
// One new TextView will also be assigned an id==12:
tv.setId(i);
placeholder.addView(tv);
}
所以这两个占位符
和我们新的的TextView
■一个有一个 ID
12>!但是,这是不是一个真正的问题,如果我们查询占位符的子观点:
So placeholder
and one of our new TextView
s both have an id
of 12! But this isn't really a problem if we query placeholder's child views:
// Will return a generated TextView:
placeholder.findViewById(12);
// Whereas this will return the ViewGroup *placeholder*;
// as long as its R.id remains 12:
Activity.this.findViewById(12);
*没那么糟
*Not so bad