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

标签 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>

如何让listview的高度与剩下的parent匹配?

如果我给listview设置一个高度

            android:layout_height="400dp"

这并不适合所有设备

如果我设置 match_parent

            android:layout_height="match_parent"

ListView 将覆盖底部的按钮

最佳答案

如果您将其高度设置为 0dp 并将其布局权重设置为 1,它将填充剩余空间。此外,您不需要围绕 ListViewLinearLayout

<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>

关于Android线性布局,如何匹配剩余的父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299941/

相关文章:

Android:带有下拉详细 View 的 ListView (不是微调器或不可扩展的 ListView )

android - 尝试以编程方式修复 sqlite 数据库

FragmentActivity 中没有图库的 Android Image Slider

android-studio - ListView 是 Android Studio 5 中的旧版本,我正在尝试做可滚动列表,那么该使用什么?

java - 设置闹钟时,如何在 HTC WorldClock.apk 中创建垂直无限 ScrollView ?

android - ListView 每页仅显示一个数据

android - 如何在按钮上显示阿拉伯数字?

android - 更改 Android 谷歌地图的标记范围

android - 在选项卡中滑动时如何避免 onCreateView()

android - 删除 ListView 和sqlite的行