maxHeight不适用于RecyclerView

Pet*_*der 3 android android-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>
Run Code Online (Sandbox Code Playgroud)

我想最初"包装回收器视图内容"(高度),但它不能超过280dp.似乎android:maxHeight没有任何影响.

Ben*_* P. 15

只要您的父布局是您的,您就可以使用XML实现此目的ConstraintLayout.对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/>
Run Code Online (Sandbox Code Playgroud)

关键属性是这三个:

android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
Run Code Online (Sandbox Code Playgroud)

通常,0dp在约束视图的两侧时只使用维度,但实际上您所要做的就是提供足够的约束来定义视图大小(并且约束双方只是最简单的方法).将高度设置为"匹配约束"后,您可以将默认高度约束与最大高度约束相结合,以获得您正在寻找的内容.

请注意,您必须使用1.1版本的约束布局库.确保您的应用的build.gradle文件具有以下内容:

implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'
Run Code Online (Sandbox Code Playgroud)

  • `layout_constraintHeight_default="wrap"` 已弃用。请改用 `layout_height="WRAP_CONTENT"` 和 `layout_constrainedHeight="true"`。 (4认同)

小智 5

尝试这个。对于那些没有参与预览答案的人。

经过一番尝试和错误。尝试添加 app:layout_constraintHeight_min="100dp"

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="wrap"
            app:layout_constraintHeight_max="400dp"
            app:layout_constraintHeight_min="100dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:listitem="@layout/list_item" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)