RecyclerView的最大高度与wrap_content

3

我在 Constraint Layout 中有一个 Recycler View,用来展示来自数据库的数据。我想将其最大高度设为 500dp,但问题是当超过这个大小时,它会从视图底部剪切一部分。

我不知道问题出在哪里。

以下是 RecyclerView 的 xml:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxHeight="500dp"
    tools:context=".histDialog">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="3dp"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView出现在一个样式为对话框的活动中。

我尝试了我所能想到的一切,但是它就是无法工作。

1个回答

3
我找到了解决方案。这是XML代码:
<?xml version="1.0" encoding="utf-8"?>

<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:layout_width="wrap_content"
    android:layout_height="wrap_content"

    tools:context=".histDialog">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        android:clipToPadding="true"
        android:padding="3dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="500dp"
        app:layout_constraintHeight_min="50dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

重要的部分是:
        android:layout_height="0dp"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="500dp"
        app:layout_constraintHeight_min="50dp"

针对RecyclerView做出了改进。花费了我很长时间才意识到这是如此简单的事情。希望这能够帮助其他人。


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