【问题标题】:What is the difference between @id and @+id?@id 和@+id 有什么区别?
【发布时间】:2012-06-22 17:09:33
【问题描述】:

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

【问题讨论】:

    标签: android xml


    【解决方案1】:

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

    完全来自 docs

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

    这是一个实际的例子:

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

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

    而这个android:layout_below="@id/start" 指的是现有的id.start,这意味着您的ID 为checkButton 将位于Button 之下,ID 为start.

    【讨论】:

    • 好的,谢谢,这是否意味着我可以随时使用@+id 没有问题?
    • 是的,当您想创建新的唯一 id 时,只需按照文档的意思使用 @+id。
    • @Sajmon 当我使用 android:layout_below="@+id/start" 而不是 android:layout_below="@id/start" 时会产生问题。或者它会在很长一段时间后产生问题?
    • ID 是在编译时一起生成的,还是在某种意义上顺序很重要?我习惯于只对新 id 的第一个实例使用 +,但是当我有类似 android:layout_below="@+id/start" 的内容后跟 android:id="@id/start" 的实际视图时,预览器往往会感到困惑并且不会创建显示。解决方法是要么同时拥有@+id,要么在@+id/start之前拥有@id
    • 如果我以后需要更改视图的顺序怎么办?在那种情况下,我应该重构所有的 ID。这就是为什么我尽可能使用@+id 。只是对性能感兴趣,如果这是系统检查重复项的开销。我认为没有。
    【解决方案2】:

    所有其他答案都忘了提到这件小事。

    当使用 @id/ 来引用已经生成的 android 资源时,请确保您引用的资源是在之前而不是之后定义的。

    那不是这个:

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

    使用这个:

    <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/start。 虽然这在原生 android 的情况下可以工作,但如果你打算在 react-native 或 ionic 或任何其他混合平台上使用此代码,它会产生资源未找到错误。

    所以在使用它作为@id/

    之前要小心生成资源id

    【讨论】:

      【解决方案3】:

      android:id="@+id/my_button"

      +id 加 sing 告诉 android 在 Resources 中添加或创建一个新的 id。

      android:layout_below="@id/my_button"

      它只是帮助引用已经生成的 id..

      【讨论】:

        【解决方案4】:

        有时您必须使用 + 号。例如。当您使用&lt;include ... /&gt; 并且包含的​​文件如下所示:

        <?xml version="1.0" encoding="utf-8"?>
        <android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            (...)
            app:layout_anchor="@+id/view_pager"
            app:layout_anchorGravity="top|right|end"
         />
        

        如果你没有在"@+id/view_pager" 中添加+,你会在构建项目时出错:

        Error:(9, 24) No resource found that matches the given name (at 'layout_anchor' with value '@id/view_pager').
        

        这发生在我的图书馆项目中。

        【讨论】:

          【解决方案5】:

          为了在 Java 中访问一个小部件(或组件)或让其他人依赖它,我们需要一个唯一的值来表示它。该唯一值由 android:id 属性提供,该属性实质上将作为 @+id/ 后缀提供的 id 添加到 id 资源文件中,以供其他人查询。 Toolbar 的 id 可以这样定义,

          android:id=”@+id/toolbar
          

          现在可以通过 findViewById(...) 跟踪以下 id,它在 res 文件中查找它的 id,或者只是 R.id 目录并返回相关视图的类型。 另一个 @id 与 findViewById(...)  的行为相同—— 通过提供的 id 查找组件,但仅保留用于布局。它最一般的用途是相对于它返回的组件放置一个组件。

          android:layout_below=”@id/toolbar”
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-05-20
            • 2015-10-14
            • 1970-01-01
            • 2012-10-06
            • 2010-10-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多