为什么在 id 属性之外允许声明 @+id
Why declaring @+id is allowed in other than id attributes
所以最后我不得不为 android 应用重建我的 XML 布局文件。我看到了像
这样的结构
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/rev_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rev_arrow">
<!-- some stuff in here -->
</LinearLayout>
<ImageView
android:id="@+id/rev_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
/>
</RelativeLayout>
当我看到有人在not android:id属性中使用@+id时,真的很烦。当任何开发人员想要搜索此 id 时,他将首先找到 LinearLayout 而不是 ImageView。
我的问题是为什么 google 允许它?有没有什么特别的原因,我只是不知道?
抱歉我的英语不好。
这是对 ID 的有效使用,因为它告诉布局管理器由 id/rev_main
视图标识的视图将放置在由 id/rev_arrow
标识的视图下方。
所以在 android:id
以外的地方,id 被用作对相应 id 标识的视图的引用。
是的,有一个原因:
有时,您必须相对于稍后在 XML 文件中声明的视图 B 的位置设置视图 A(在您的示例中就是这种情况。LinearLayout 是 "View A" 和 ImageView是 "View B").
想象一下,您遇到问题的代码
android:layout_below="@+id/rev_arrow"
看起来像这样:
android:layout_above="@+id/rev_arrow"
如果你不能在其中声明一个 id,android:layout_above 将毫无用处。
因为有几条评论提到了:
您必须始终在那个地方使用 "plus" 符号,在布局文件中首先声明 id 的地方(从上到下)。它独立于属性,如 "id" 或 "layout_below".
所以最后我不得不为 android 应用重建我的 XML 布局文件。我看到了像
这样的结构<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/rev_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rev_arrow">
<!-- some stuff in here -->
</LinearLayout>
<ImageView
android:id="@+id/rev_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
/>
</RelativeLayout>
当我看到有人在not android:id属性中使用@+id时,真的很烦。当任何开发人员想要搜索此 id 时,他将首先找到 LinearLayout 而不是 ImageView。 我的问题是为什么 google 允许它?有没有什么特别的原因,我只是不知道?
抱歉我的英语不好。
这是对 ID 的有效使用,因为它告诉布局管理器由 id/rev_main
视图标识的视图将放置在由 id/rev_arrow
标识的视图下方。
所以在 android:id
以外的地方,id 被用作对相应 id 标识的视图的引用。
是的,有一个原因: 有时,您必须相对于稍后在 XML 文件中声明的视图 B 的位置设置视图 A(在您的示例中就是这种情况。LinearLayout 是 "View A" 和 ImageView是 "View B").
想象一下,您遇到问题的代码
android:layout_below="@+id/rev_arrow"
看起来像这样:
android:layout_above="@+id/rev_arrow"
如果你不能在其中声明一个 id,android:layout_above 将毫无用处。
因为有几条评论提到了: 您必须始终在那个地方使用 "plus" 符号,在布局文件中首先声明 id 的地方(从上到下)。它独立于属性,如 "id" 或 "layout_below".