【问题标题】:ListView takes up all space in LinearLayoutListView 占据了 LinearLayout 中的所有空间
【发布时间】:2015-12-18 15:19:11
【问题描述】:

我有这个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 占据整个布局,带有EditTextLinearLayout 被添加到屏幕底部边缘下方并且不可见。我怎样才能解决这个问题?我尝试了layout_weight,但没有成功。

【问题讨论】:

  • 顺便说一下,match_parentfill_parent 做同样的事情。无需同时使用它们。事实上,您应该只使用match_parent,因为fill_parent 在 API 8 中已被弃用。
  • @NoChinDeluxe 感谢您的澄清,我认为它们是不同的。

标签: android android-layout android-listview android-linearlayout


【解决方案1】:

那是因为您使用的是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 分钟内接受
  • fill_parent 已弃用,等效于match_parent,请改用后者。
  • 也许这就是为什么不推荐使用 fill_parent 的原因,因为它具有误导性:)
  • 从来没有考虑过这个。有道理!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多