@id/和@+id/有什么区别?

在@+id/中,加号+指示创建一个新的资源名并添加到R.java文件中,但是@id/呢?从ID的文档:当引用一个Android资源ID时,你不需要加号,但必须添加Android包的命名空间,如下所示:

android:id="@android:id/list"

但是在下图中,Eclipse并没有建议使用任何@android:id/。

@id/和@android:id/相同吗?


当前回答

Eclipse中有一个bug,有时如果你创建了一个新的@+id/..,它不会立即添加到R.java文件中,即使在清理构建项目之后也是如此。解决方案是重新启动Eclipse。

我认为这个问题应该尽快解决,因为它可能(从经验来看,将会)使一些开发人员感到困惑,认为他们的语法有问题,并试图调试它,即使实际上没有什么需要调试的。

其他回答

The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.

来自:https://developer.android.com/training/basics/firstapp/building-ui.html

Android使用一些称为资源的文件,其中存储XML文件的值。

现在,当您对一个XML对象使用@id/时,它试图引用一个已经在值文件中注册的id。另一方面,当你使用@+id/时,它会在值文件中注册一个新的id,就像'+'符号所暗示的那样。

希望这能有所帮助:)。

+号是将id添加到资源id列表的快捷方式。否则,您需要将它们保存在这样的xml文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="my_logo" type="id"/>
</resources>

@+id和@id的区别是:

@+id用于在R.java文件中为视图创建id。 @id用于引用在R.java文件中为视图创建的id。

我们使用@+id与android:id="",但如果id没有创建,我们在创建之前引用它(前向引用)。

在这种情况下,我们使用@+id来创建id,在定义视图时,我们必须引用它。

请参考以下代码:

<RelativeLayout>

     <TextView
        android:id="@+id/dates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/spinner" />

   <Spinner
     android:id="@id/spinner"
     android:layout_width="96dp"
     android:layout_height="wrap_content"
     android:layout_below="@id/dates"
     android:layout_alignParentRight="true" />

</RelativeLayout>

在上面的代码中,Spinner @+id/ Spinner的id是在另一个视图中创建的,在定义Spinner时,我们引用了上面创建的id。

如果我们在视图被创建之前使用视图,我们必须创建id。

简而言之

android:id="@+id/my_button"

+id加号告诉android在资源中添加或创建一个新的id。

android:layout_below="@id/my_button"

它只是帮助引用已经生成的id..