为什么如果我将View放在RelativeLayout的底部,RelativeLayout会填满所有空间?

时间:2013-10-15 11:11:35

标签: android xml android-layout

我正在尝试构建一个在我的应用中使用的“行”。

在“行”中,我使用两个视图来创建一个水平条。

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >

<TextView
    android:id="@+id/eventname"
    style="@style/T2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="nome evento" >
</TextView>

<TextView
    android:id="@+id/eventtype"
    style="@style/S2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:text="tipo evento" />

<View
    android:id="@+id/riga"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_alignParentTop="true"
    android:background="#ffffff" />

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_above="@+id/riga"
    android:background="@color/lines" />

</RelativeLayout>

这是我得到的结果:

enter image description here

到目前为止一切顺利。

但如果我把视图置于底部:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >

<TextView
    android:id="@+id/eventname"
    style="@style/T2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="nome evento" >
</TextView>

<TextView
    android:id="@+id/eventtype"
    style="@style/S2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:text="tipo evento" />

<View
    android:id="@+id/riga"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_alignParentBottom="true"
    android:background="#ffffff" />

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_above="@+id/riga"
    android:background="@color/lines" />

</RelativeLayout>

这是我得到的结果

enter image description here

请问我该怎么解决这个????

谢谢!!!

2 个答案:

答案 0 :(得分:2)

解决!!!!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >

<TextView
    android:id="@+id/eventname"
    style="@style/T2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="nome evento" >
</TextView>

<TextView
    android:id="@+id/eventtype"
    style="@style/S2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:text="tipo evento" />

<View
    android:id="@+id/rigagrigia"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/eventtype"
    android:background="@color/lines" />

<View
    android:id="@+id/rigabianca"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@id/rigagrigia"
    android:background="#ffffff" />

</RelativeLayout>

答案 1 :(得分:1)

将布局更改为:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >

    <TextView
        android:id="@+id/eventname"
        style="@style/T2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="nome evento" >
    </TextView>

    <TextView
        android:id="@+id/eventtype"
        style="@style/S2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_below="@id/eventname"
        android:text="tipo evento" />

    <View
        android:id="@+id/riga"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="#ffffff" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_above="@id/riga"
        android:background="@color/lines" />

</RelativeLayout>

riga已经宣布,因此您无法再次使用@+id。要引用已创建的ID,请使用@id/riga