android - 为什么设计者使用 "@+id"而不是 "@id"进行约束?

标签 android android-studio android-layout designer

我一直都懂@+id表示创建新 ID(通常与 android:id 一起使用)和 @id在其他地方引用它,如详细解释 in a popular question herethe 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"
在哪里 rlistfragandroid:id 分配给另一个元素在同一个文件中——如果它不存在,则设计者不可能创建约束,因此不会有上下文歧义。1 根据文档,这相当于两次声明“必须创建的新资源名称并添加到我们的资源中”。
似乎这里的语义至少与文档中描述的不完全一样。设计师为什么要这样做,那些省略的语义是什么?
  • 或者那里可以?这一切对我来说意味着元素可以按任何顺序处理,所以关键是每当第一次遇到一个 id 时,它都会被创建,即使它与现有元素没有关联。

  • 文档中有一点 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/

    相关文章:

    android-studio - 如何在 Android Studio 中对齐/格式化代码?

    java - 使用 Android Studio : NoClassDefFoundError: org/testng/TestNG 进行测试

    java - 如何改进进度条匹配以跟随图像(颜色、角度、形状等)

    Android 库使用自己的资源调用 setContentView,但失败并返回 "unable to resolve static field"

    java - 使用大纲文本字段的工具栏问题

    Android studio gradle异常(sdk 23.2.0、retrofit 2、google-service 8.4)

    android-studio - Android Studio 1.4 不允许 Android 模块依赖于 Java 测试模块

    Android 工具栏覆盖了一个 fragment

    java - 如何在 ListView 中通知数据集正确更改

    java - 为 RecyclerView 项目创建选项菜单