maxHeight 不适用于 RecyclerView [英] maxHeight does not work on RecyclerView
问题描述
我有一个 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>
我最初想包装回收站视图内容"(高度),但不能超过 280dp.似乎 android:maxHeight
没有任何影响.
I want to initially "wrap recycler view content" (height), but it cannot exceed 280dp. It seems that android:maxHeight
has no effect what-so-ever.
推荐答案
只要您的父布局是 ConstraintLayout
,您就可以仅使用 XML 来实现这一点,这似乎是您的布局.对您的 RecyclerView
标签进行以下更改:
You can achieve this using only XML as long as your parent layout is ConstraintLayout
, which yours seems to be. Make these changes to your RecyclerView
tag:
<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"
通常情况下,当您约束视图的两侧时,您只使用 0dp
维度,但实际上您所要做的就是提供足够的约束来定义视图大小(并约束双方只是最简单的方法).将高度设置为匹配约束"后,您可以将默认高度约束与最大高度约束结合起来,以准确获得所需的高度.
Normally you only use a 0dp
dimension when you constrain both sides of the view, but really all you have to do is provide enough constraints to define the view size (and constraining both sides is just the easiest way to do that). Once you have the height set to "match constraints", you can combine a default height constraint with a maximum height constraint to get exactly what you're looking for.
请注意,您必须为此使用 1.1 版本的约束布局库.确保您的应用的 build.gradle 文件具有以下内容:
Note you must be using a 1.1 version of the constraint layout library for this. Make sure your app's build.gradle file has something like:
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'
这篇关于maxHeight 不适用于 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!