9

我正在尝试将一个 TextView 放在顶部,另一个放在底部,并在使用剩余空间之间放置一个 ListView。但是,我的 ListView 将所有空间都放到了底部,使得最后一个 TextView 甚至不显示。我可以让它显示的唯一方法是给 ListView 一个固定的高度,比如 100dp。

我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/id_task_list_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_task_list"
        style="?android:attr/listSeparatorTextViewStyle" />

    <ListView
        android:id="@+id/id_task_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_task_list_title" />

    <TextView
        android:id="@+id/id_task_list_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_task_list_view"
        android:text="OMG Test" />

</RelativeLayout>
4

5 回答 5

10

使用 LinearLayout 如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity"
                android:orientation="vertical">

    <TextView
            android:id="@+id/id_task_list_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title_task_list"
            style="?android:attr/listSeparatorTextViewStyle" />

    <ListView
            android:id="@+id/id_task_list_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    <TextView
            android:id="@+id/id_task_list_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="OMG Test" />

</LinearLayout>
于 2013-10-23T10:19:52.340 回答
1

这应该通过使用 android:layout_above 和 android:layout_below 将 ListView 放置在 TextViews 之间来解决您的问题:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/id_task_list_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_task_list"
        style="?android:attr/listSeparatorTextViewStyle" />

    <TextView
        android:id="@+id/id_task_list_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="OMG Test" />

    <ListView
        android:id="@+id/id_task_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/id_task_list_test"
        android:layout_below="@id/id_task_list_title"/>


</RelativeLayout>
于 2013-10-23T10:20:52.083 回答
0

可以通过以下两种方式之一实现

1)使用线性布局(直截了当)

2)使用相对布局并参考相对于其他视图的视图位置。

在你的情况下

1)改变相对于LinearLayout,工作就完成了。

2)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TextView
            android:id="@+id/id_task_list_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title_task_list"
            style="?android:attr/listSeparatorTextViewStyle" />

        <ListView
            android:layout_below="@+id/id_task_list_title"
            android:id="@+id/id_task_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/id_task_list_title" />

        <TextView
            android:layout_below="@+id/id_task_list_view"
            android:id="@+id/id_task_list_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/id_task_list_view"
            android:text="OMG Test" />

    </RelativeLayout>
于 2013-10-23T10:19:10.260 回答
0

这可以通过编码来完成,方法是获取屏幕的高度并将布局参数宽度分配给这样的列表视图

列表视图的高度=屏幕高度-(textView1的高度+textView2的高度)。

于 2013-10-23T10:39:12.563 回答
0

而不是使用 RelativeLayout ,而是使用具有垂直方向的 LinearLayout。

我认为它可以解决您的问题

谢谢

于 2013-11-16T07:02:26.593 回答