maxHeight不适用于RecyclerView

时间:2018-03-23 12:36:15

标签: 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>

我想最初&#34;包装回收者视图内容&#34; (高度),但不能超过280dp。 android:maxHeight似乎没有任何影响。

5 个答案:

答案 0 :(得分:11)

只要您的父布局为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'

答案 1 :(得分:2)

如果要使用XML文件实现此目的,请按以下步骤操作。 这是针对androidx组件的。首先,您需要在Gradle文件中实现Beta版本

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta6'

此后,使用以下代码创建最大高度为240dp的recyclerView

 <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="240dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>

答案 2 :(得分:1)

我真的为此感到挣扎。请注意,如果您使用的是androidx,则有些不同: -首先使用最新的依赖,即时通讯使用 实现“ androidx.constraintlayout:constraintlayout:2.0.0-beta4” -第二点,在androidx中,标签有些不同

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_tilemap"
android:layout_width="match_parent"
android:layout_height="wrap_content">


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="280dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/rv_comms"
        android:padding="8dp"
        android:clipToPadding="false" />

</androidx.constraintlayout.widget.ConstraintLayout>

这终于对我有用。

答案 3 :(得分:1)

尝试一下。对于那些没有使用预览功能的人。

经过反复试验。尝试添加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>

答案 4 :(得分:0)

我遇到了同样的问题。我扩展了以下RecyclerViewas

public class MaxHeightRecyclerView extends RecyclerView {
    private static final int MAX_HEIGHT = 765;

    public MaxHeightRecyclerView(Context context) {
        super(context);
    }

    public MaxHeightRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MaxHeightRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        heightSpec = MeasureSpec.makeMeasureSpec((int) (MAX_HEIGHT / Resources.getSystem().getDisplayMetrics().density), MeasureSpec.AT_MOST);
        super.onMeasure(widthSpec, heightSpec);
    }
}
相关问题