我有这个LinearLayout包含一个ListView和一个LinearLayout

<?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占据了整个布局,LinearLayoutEditText被添加到屏幕底部边缘下方,不可见。我该怎么解决?我试过使用layout_weight但没有成功。

最佳答案

这是因为您使用的是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>

另一种选择是使用相对延迟。只要listview的高度不小于aa>,就应该是正常的。
<?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>

07-24 09:49
查看更多