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

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

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

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

@id/和@android: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”

其他回答

很简单:

“@ +……”-创建新的

“@……”-现有链接

来源:https://developer.android.com/guide/topics/resources/layout-resource.html idvalue

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

你引用Android资源,这些资源已经在Android系统中定义了,使用@android:id/..当访问您在项目中定义/创建的资源时,使用@id/..

更多信息

根据你在聊天中的澄清,你说你有一个这样的问题:

如果我们使用android:id="@id/layout_item_id",这是行不通的。取而代之的是@+id/,那么这里有什么不同呢?这就是我最初的问题。

好吧,这取决于上下文,当你使用android:id的XML属性时,你指定了一个新的id,并指示解析器(或称其为构建器)在R.java中创建一个新条目,因此你必须包含一个+号。

而在另一种情况下,像android:layout_below="@id/myTextView",你引用的是一个已经创建的id,所以解析器链接到这个已经创建的id在R.java。

更多信息

正如你在聊天中所说,注意android:layout_below="@id/myTextView"不会识别id myTextView的元素,如果它写在你正在使用它的元素之后。

简而言之

android:id="@+id/my_button"

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

android:layout_below="@id/my_button"

它只是帮助引用已经生成的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”