maxHeight在RecyclerView上不起作用 [英] maxHeight does not work on RecyclerView

查看:633
本文介绍了maxHeight在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆