gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:58 25 4
gpt4 key购买 nike

我要创建一个底部有两个按钮的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com