android – 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没有任何影响.

解决方法:

只要你的父布局是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"

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

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

implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...