“@id/"之间的区别和“@+id/"在安卓中 [英] Difference between "@id/" and "@+id/" in Android

查看:34
本文介绍了“@id/"之间的区别和“@+id/"在安卓中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

In @+id/ the plus symbol + instructs to create a new resource name and add in to the R.java file but what about @id/? From the documentation of ID: 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/list"

但是在下面的图片中,Eclipse 不建议使用任何类型的 @android:id/.

But in the image below Eclipse doesn't suggest any kind of @android:id/.

@id/@android:id/ 是否相同?

推荐答案

你参考了Android资源,Android系统中已经定义好了,@android:id/.. 而要访问您在项目中定义/创建的资源,您可以使用 @id/..

you refer to Android resources , which are already defined in Android system, with @android:id/.. while to access resources that you have defined/created in your project, you use @id/..

更多信息

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

As per your clarifications in the chat, you said you have a problem like this :

如果我们使用 android:id="@id/layout_item_id" 它不起作用.相反 @+id/ 有效,那么这里有什么区别?这就是我最初的问题.

If we use android:id="@id/layout_item_id" it doesn't work. Instead @+id/ works so what's the difference here? And that was my original question.

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

Well, it depends on the context, when you're using the XML attribute of android:id, then you're specifying a new id, and are instructing the parser (or call it the builder) to create a new entry in R.java, thus you have to include a + sign.

而在另一种情况下,例如 android:layout_below="@id/myTextView" ,您指的是已创建的 id,因此解析器将其链接到已创建的 id在 R.java.

While in the other case, like android:layout_below="@id/myTextView" , you're referring to an id that has already been created, so parser links this to the already created id in R.java.

再次了解更多信息

正如您在聊天中所说,请注意 android:layout_below="@id/myTextView" 将无法识别具有 id myTextView 的元素,如果它写在您在其中使用它的元素.

As you said in your chat, note that android:layout_below="@id/myTextView" won't recognize an element with id myTextViewif it is written after the element you're using it in.

这篇关于“@id/"之间的区别和“@+id/"在安卓中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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