什么是betwenn @ ID /和@ + id /下的差异机器人? [英] what is the difference betwenn @id/ and @+id/ in android?

查看:349
本文介绍了什么是betwenn @ ID /和@ + id /下的差异机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/3264235/what-is-different-between-id-androidlist-and-id-androidlist">What是不同的@ + ID / Android的:清单和@ ID / Android的:清单??

@id /.. 和@ + ID /..的区别是什么?我不是指之间的差异 @android:ID /.. @id /..

What is the difference between @id/.. and @+id/..? I am not referring to the difference between @android:id/.. and @id/..

code例:

<Button
android:id ="@id/add_button"
/>
<Button
android:id ="@+id/remove_button"
/>

两个 ID之间的区别是什么定义上面?

推荐答案

您必须使用 @ + 符号上的标识第一次出现在一个XML文件中。第二次以后,你可以 - 也应该 - 掉落 + 标志。这将有助于赶上错别字。

You must use the @+ notation on the first occurrence of the ID within an XML file. The second and subsequent times you can -- and should -- drop off the + sign. This will help catch typos.

例如,假设你有一个 RelativeLayout的。你有一个的TextView RelativeLayout的机器人:ID @ + /标记。后来的布局XML文件中,要引用到的TextView 从另外一个用于定位的目的(例如,安卓layout_below )。

For example, suppose that you have a RelativeLayout. You have a TextView in that RelativeLayout whose android:id is @+id/label. Later on in the layout XML file, you want to refer to that TextView from another one for positioning purposes (e.g., for android:layout_below).

如果您在键入的android:layout_below =@ + ID / labbel(注意错字),在编译时,这将被认为是OK。然而,在运行时,事情将无法正常工作,包括从部件被不正确地定位到彻底崩溃,这取决于Android版本。

If you typed in android:layout_below="@+id/labbel" (note the typo), at compile time, this will be considered OK. However, at runtime, things will not work, ranging from the widget being positioned incorrectly to an outright crash, depending on Android version.

如果您在键入的android:layout_below =@ ID / labbel(注意错字的的缺失 + 符号),那么你会得到一个编译错误。

If you typed in android:layout_below="@id/labbel" (note the typo and the missing + sign), then you would get a compile error.

更新

由于我是不够明确的第一次,很显然,让我们再试一次。

Since I wasn't clear enough the first time, apparently, let's try again.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="URL:"
        android:layout_alignBaseline="@+id/entry"
        android:layout_alignParentLeft="true"/>
    <EditText
        android:id="@id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/label"
        android:layout_alignParentTop="true"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignRight="@id/entry"
        android:text="OK" />
    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

在上面,你会看到一个 RelativeLayout的。你会发现,每个ID的第一个出现获得 + 标志。每个ID的第二个和以后的事件没有得到 + 标志。

Above, you will see a RelativeLayout. You will notice that the first occurrences of each ID get the + sign. The second and subsequent occurrences of each ID do not get the + sign.

您可以使用 + 标志对所有的人,但如果你犯了一个错字,编译器不会赶上问题。

You could use the + sign on all of them, but then if you make a typo, the compiler will not catch the problem.

+ 有效签署声明分配一个新的ID。不具有 + 签署国使用previously分配的ID,或未能在编译时,如果没有这样的ID。

The + sign effectively states "allocate a new ID". Not having the + sign states "use a previously-allocated ID, or fail at compile time if there is no such ID".

这篇关于什么是betwenn @ ID /和@ + id /下的差异机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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