今天敲代码的时候发现突然想起,一直在 XML 布局文件中为每个组件赋值 ID 的时候是使用的@+id/来定义 ID,但是就突然疑惑了,为什么是@+id/,而不是@id/?果断的 Google 大法,找到了 StackOverflow 上的解答 1 、 解答 2:
1 2 3 4 5 6 7 8 9
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 newresource 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.
Next
假如我强制需要使用@id/来定义组件的 ID,我应该如何做? 我们只需要在value/文件夹中定义一个 resources 的 xml 文件即可,如下:
1 2 3 4
<?xml version="1.0" encoding = "utf-8"> <resources> <item name = "custom_id" type = "id"/> </resources>