我有一个 DialogFragment
。
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxHeight="280dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line/>
<View
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="@color/black"/>
</android.support.constraint.ConstraintLayout>
我想最初“包装回收站 View 内容”(高度),但它不能超过 280dp。似乎 android:maxHeight
没有任何效果。
最佳答案
只要您的父布局是 ConstraintLayout
,您就可以仅使用 XML 来实现这一点,而您的父布局似乎是。对您的 RecyclerView
标记进行以下更改:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line/>
关键属性是这三个:
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
通常,当您约束 View 的两侧时,您只使用 0dp
维度,但实际上您所要做的就是提供足够的约束来定义 View 大小(并且限制双方只是做到这一点的最简单方法)。将高度设置为“匹配约束”后,您可以将默认高度约束与最大高度约束结合起来,以准确获得您要查找的内容。
请注意,您必须为此使用 1.1 版的约束布局库。确保您应用的 build.gradle 文件包含如下内容:
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'
关于android - maxHeight 不适用于 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49449828/