布局ID必须在指定后再次添加

时间:2017-12-13 12:47:34

标签: android layout

我有这个非常简单的layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp">

<TextView
    android:id="@+id/textViewBottom"
    style="@style/NLBTextAppearanceSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Information"/>

<TextView
    android:id="@+id/textViewTop"
    style="@style/NLBTextAppearance"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur condimentum tortor quis quam hendrerit, a rhoncus mauris porta. In at nisl et arcu consequat placerat. Integer in ipsum lectus. Proin elementum faucibus odio, in sodales dui tristique eget"
    android:layout_above="@+id/textViewBottom" />

</RelativeLayout>

我不明白为什么在@+id/textViewBottom已添加android:layout_above of textViewTop;之后我必须在id再次textViewBottom。 如果我从该行中删除“+”符号,则视图会相互重叠。我以前从来没有遇到过这个问题...

3 个答案:

答案 0 :(得分:1)

您正在android:layout_above="@+id/textViewBottomTextview置于名为 textViewBottom TextView之上。

RelativeLayout的属性只需彻底Relativelayout及其基本属性就可以清楚地理解它。

答案 1 :(得分:1)

如果您在id中使用RelativeLayout作为参考,那么只有 id 已经提供给@id/而不是@+id/任何VIew

这意味着您的R.java文件已经注册了Id,这就是为什么只需要使用@id/

答案 2 :(得分:0)

构建应用程序时,它会创建一个文件来引用布局中添加的所有ID。然后,您可以使用这些ID来检索视图或根据另一个视图放置视图。

+id/仅使用它时,id/会将ID添加到此引用列表中。

在声明之前你不能使用引用,所以第一次应该始终是+id/,即使它不在id定义的视图上。

如果我使用你的例子:

<!-- VIEW 1 -->
<TextView
    android:id="@+id/textViewBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#fff9e5"
    android:text="Information" />

<!-- VIEW 2 -->
<TextView
    android:id="@+id/textViewTop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/textViewBottom"
    android:background="#e5ffe5"
    android:text="Lorem ipsum dolor sit amet" />

VIEW 1声明了身份textViewBottom,因此,当您想要将VIEW 2置于其上方时,您只需使用id作为textViewBottom已经宣布了。

如果你改变了这样的观点的顺序:

<!-- VIEW 2 -->
<TextView
    android:id="@+id/textViewTop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/textViewBottom"
    android:background="#e5ffe5"
    android:text="Lorem ipsum dolor sit amet" />


<!-- VIEW 1 -->
<TextView
    android:id="@id/textViewBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#fff9e5"
    android:text="Information" />

现在VIEW 2需要声明textViewBottom才能使用它,因为它之前从未声明过。否则视图将无法正确放置。所以,现在,当你想在VIEW 1上添加id时,你不需要+id/,因为id已经被声明了。