我一直都懂@+id
表示创建新 ID(通常与 android:id
一起使用)和 @id
在其他地方引用它,如详细解释 in a popular question here在 the official documentation :
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...
这没有强制执行有点奇怪。你可以使用
@+id
无处不在,多次使用相同的 id,这似乎不是错误或问题(除非您使用相同的 id 和多个 android:id
声明)。当在 Android Studio 中使用带有约束布局的图形设计器时(实际上它可能包含所有内容,我没有检查过),每当添加约束时,它都会像这样声明它们:
app:layout_constraintTop_toBottomOf="@+id/rlistfrag"
在哪里 rlistfrag
用 android:id
分配给另一个元素在同一个文件中——如果它不存在,则设计者不可能创建约束,因此不会有上下文歧义。1 根据文档,这相当于两次声明“必须创建的新资源名称并添加到我们的资源中”。似乎这里的语义至少与文档中描述的不完全一样。设计师为什么要这样做,那些省略的语义是什么?
文档中有一点 guide on layout resources确认 id 只会在第一次设置:
The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist.
这并不意味着元素没有按顺序处理。
最佳答案
它适用于所有布局组件。每当您想提及“id”时,都必须使用“@+id”
关于android - 为什么设计者使用 "@+id"而不是 "@id"进行约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63328438/