@id 和@+id 有什么区别? [英] What is the difference between @id and @+id?

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

问题描述

我刚刚开始使用 android,完成了大约 5 个布局文件.但是,我刚刚意识到我一直在交替使用@id 和@+id,但我不确定两者之间的确切区别是什么.

I have just started using android, and have about 5 layout files finished. However, I just realized that I have been using @id and @+id interchangeably, but I'm not sure what the exact difference between the two are.

推荐答案

在为视图定义自己的 ID 时需要使用 @+id.

You need to use @+id when you are defining your own Id for a View.

完全来自文档:

字符串开头的 at 符号 (@) 表示XML 解析器应该解析和扩展 ID 字符串的其余部分和将其标识为 ID 资源.加号 (+) 表示这是必须创建并添加到我们的资源中的新资源名称(在 R.java 文件中).还有许多其他 ID 资源由 Android 框架提供.引用 Android 时资源ID,不需要加号,但必须加上android包命名空间.

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.

这是一个实际的例子:

<Button 
   android:id="@+id/start"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
/>

<Button 
   android:id="@+id/check"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@id/start"
/>

所以在这里,您创建了两个 ID,开始并检查.然后,在您的应用程序中,您可以使用 findViewById(R.id.start) 连接到它们.

而这个 android:layout_below="@id/start" 指的是现有的 id.start 并且意味着你的 Button id check 将位于 Button 下方,id 为 start.

So here, you created two IDs, start and check. Then, in your application you are able to connect to them with findViewById(R.id.start).

And this android:layout_below="@id/start" refer to existing id.start and means that your Button with id check will be positioned below Button with id start.

这篇关于@id 和@+id 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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