【问题标题】:Listview size adapt on items列表视图大小适应项目
【发布时间】:2020-03-18 11:18:19
【问题描述】:

我对列表视图的设计有疑问。

当有几个项目时,我想减少列表,但如果我将包装内容放在屏幕中间。

我该怎么办?

我的列表视图:

<ListView
        android:id="@+id/listView_location"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/searchView_location"
        android:layout_marginBottom="8dp"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@+id/searchView_location"
        app:layout_constraintEnd_toEndOf="@+id/searchView_location"
        app:layout_constraintStart_toStartOf="@+id/searchView_location"
        app:layout_constraintTop_toTopOf="parent"/>

约束布局

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout_search"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true">

【问题讨论】:

  • "ListView" 正在占用其父级 "constraintlayout" 的高度,请分享 listview 的父级布局
  • 将其高度更改为 wrap_content 即 contraintlayout
  • @xlu 谢谢,但约束布局上的 android:layout_height="wrap_content" 也有同样的问题

标签: android android-layout listview


【解决方案1】:

改变你的 ListView 高度

android:layout_height="wrap_content"

移除顶部约束

app:layout_constraintTop_toTopOf="parent"

【讨论】:

  • 在这种情况下,您只需要删除一个顶部约束。我更新了我的答案
  • @Rankov 感谢它的工作,但是当列表太大时它超出了屏幕,以避免我将列表限制为 6 个项目,但我想必须有更好的方法来做到这一点: )
【解决方案2】:

将其父高度更改为 wrap_content 即 contraintlayout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多