我如何可以分配一个ID到一个视图编程? [英] How can I assign an ID to a view programmatically?

查看:341
本文介绍了我如何可以分配一个ID到一个视图编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个XML文件中,我们可以分配一个ID像 Android的一个观点:ID =@ + ID /某事,然后调用 findViewById(),但创建编程视图时,我该如何分配一个ID?

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?

我觉得 SETID()是不一样的默认分配。 SETID()是多余的。

I think setId() is not the same as default assignment. setId() is extra.

有人可以纠正我?

推荐答案

这是Android的 ID 是一个整数,通常用来确定意见;这个 ID 可以通过XML被分配(如果可能),并通过code(编程)的 ID 是获取用于XML定义的查看 S由一个充气(如生成使用<$ C $引用最有用C>的setContentView )

Android id overview

An 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 Views generated by an Inflater (such as by using setContentView.)

  • 添加的 Android的一个属性:ID =@ + ID / somename 到视图
  • 当你的应用程序是,在机器人:ID 将被分配一个唯一 INT 在code使用。
  • 引用您的机器人:ID INT 用,在code值 R.id。 somename(实际上是一个常数。)
  • INT 可以从构建更改建的那么从来没有从复制一个id GEN / package.name / R.java ,只要使用 R.id。 somename。
  • (同样,一个 ID 在XML中分配给 preference 不使用时, preference 生成的查看
  • Add an attribute of android:id="@+id/somename" to your view.
  • When your application is built, the android:id will be assigned a unique int for use in code.
  • Reference your android:id's int value in code using "R.id.somename" (effectively a constant.)
  • this int can change from build to build so never copy an id from gen/package.name/R.java, just use "R.id.somename".
  • (Also, an id assigned to a Preference in XML is not used when the Preference generates its View.)
  • 手动设置 ID s,使用 someView.setId( INT );
  • INT 必须是积极的,但另有arbitrary-它可以是任何你想要的(请继续阅读,如果这是可怕的。)
  • 例如,如果要创建和编号若干意见重新presenting项目,你可以用他们的项目编号。
  • Manually set ids using someView.setId(int);
  • The int must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.)
  • For example, if creating and numbering several views representing items, you could use their item number.
  • XML -assigned ID 旨意是唯一的。
  • code-分配 ID ■不要的没有的必须是唯一
  • code-分配 ID S能(理论上)冲突 XML -assigned ID 秒。
  • 在这些相互冲突的 ID 旨意并不重要,如果查询正确的(请继续阅读)
  • XML-assigned ids will be unique.
  • Code-assigned ids do not have to be unique
  • Code-assigned ids can (theoretically) conflict with XML-assigned ids.
  • These conflicting ids won't matter if queried correctly (keep reading).
  • findViewById(INT)将遍历深度优先递归通过视图层次结构的从查看指定的,返回的第一个查看它匹配查找 ID
  • 只要没有code-分配 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.
  • As long as there are no code-assigned ids assigned before an XML-defined id in the hierarchy, findViewById(R.id.somename) will always return the XML-defined View so id'd.
  • 在布局XML,定义了一个空的的ViewGroup ID
  • 如一个的LinearLayout 机器人:ID =@ + ID /占位符
  • 使用code来填充占位符的ViewGroup 查看秒。
  • 如果您需要或想要的,指定任何 ID s表示是方便每个视图。
  • 查询中使用placeholder.findViewById(convenientInt)这些孩子的意见;

  • In layout XML, define an empty ViewGroup with id.
  • Such as a LinearLayout with android:id="@+id/placeholder".
  • Use code to populate the placeholder ViewGroup with Views.
  • If you need or want, assign any ids 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幕后冲突。

Conflicting id example

For 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 !但是,这是不是一个真正的问题,如果我们查询占位符的子观点:

So placeholder and one of our new TextViews 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

这篇关于我如何可以分配一个ID到一个视图编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆