Android线性布局,如何匹配剩余的父级?

CL *_* So 3 android android-layout android-listview android-linearlayout

我将创建一个listview,底部有两个按钮

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        <ListView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:id="@android:id/list"></ListView>
    </LinearLayout>


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:text="Add Task"
                android:id="@+id/addTaskButton"></Button>
        <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:text="Add Task2"
                android:id="@+id/addTaskButton2"></Button>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如何使listview与其余父级匹配高度?

如果我将高度设置为listview

            android:layout_height="400dp"
Run Code Online (Sandbox Code Playgroud)

这不适合所有设备

如果我设置了match_parent

            android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)

列表视图将覆盖底部的按钮

Mik*_* M. 5

如果将其高度设置为0dp并且布局权重为1,则会填充剩余空间.此外,你不需要LinearLayout周围的ListView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:text="Add Task"
            android:id="@+id/addTaskButton" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="Add Task2"
            android:id="@+id/addTaskButton2" />

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)