ListView占据了LinearLayout中的所有空间

5

我有一个包含ListViewLinearLayoutLinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:hint="Hej"/>
    </LinearLayout>
</LinearLayout>

ListView占据整个布局,而带有EditTextLinearLayout被添加到屏幕底部下方,不可见。我该如何解决这个问题?我尝试过使用layout_weight,但没有成功。


1
顺便说一下,match_parentfill_parent的作用完全相同。没有必要同时使用它们。实际上,你应该只使用match_parent,因为fill_parent在API 8中已被弃用。 - NoChinDeluxe
1
@NoChinDeluxe 感谢您的澄清,我原以为它们是不同的。 - Arbitur
1个回答

8
那是因为你正在使用fill_parent,它会做到这一点。你可以尝试类似于以下的内容...它将导致ListView扩展以填充空间。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="Hej"/>
    </LinearLayout>
</LinearLayout>

另一种选择是使用RelativeLayout。只要ListView的高度不是wrap_content,就应该没问题。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_above="@+id/footer"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="10dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="Hej"/>
    </LinearLayout>
</RelativeLayout>

谢谢,我认为它会填充尽可能多的空间而不是像match_parent一样完全填充。我会在10分钟内接受。 - Arbitur
1
fill_parent已被弃用,等同于match_parent,请使用后者。 - Mauker
也许这就是为什么fill_parent被弃用了,因为它会产生误导 :) - Arbitur
从未多想过那个问题。有道理! - Mauker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接