@Id和@ + ID之间的区别是什么?区别、Id、ID

由网友(一世终苍老.)分享简介:我刚开始使用机器人,并约5布局文件完成。然而,我才意识到,我一直在使用@Id和@ + ID互换,但我不知道这两者之间准确的区别是。I have just started using android, and have about 5 layout files finished. However, I just rea...

我刚开始使用机器人,并约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.

正好从文档

的在符号(@)在字符串的开头指示   XML解析器应该解析和扩展ID字符串的其余部分,   将其识别为一个ID的资源。的加号(+)指这是   必须创建并添加到我们的资源的新的资源名   (在R.java文件)。还有一些其他的标识资源   由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.

现在我加你实际的例子:

And now i add for you practical example:

<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"
/>

所以在这里,你创建了两个标识,启动和检查。然后,在你的应用程序,你可以连接到它们与 findViewById(R.id.start)。 而这个安卓layout_below =@ ID /启动是指现有的 id.start 并意味着你的按钮 id为查看将被定位低于按钮 id为启动

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.

阅读全文

相关推荐

最新文章