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

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

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

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

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


当前回答

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

其他回答

如果视图项执行相同的操作,您可以对任何布局中的每个条目使用@+id,因为在编译多个@+id/foo时,R.java文件只创建一个枚举。例如,如果我在每个页面上都有一个保存按钮,执行相同的操作,我使用android:id="@+id/button_save"在每个布局。R.java文件只有一个button_save条目。

@+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"

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:

android:id=“@android:id/empty”

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

@id/和@android:id/不一样。

@id/引用应用程序中的ID, @android: ID /引用Android平台中的项目。

Eclipse是错误的。