在Constraint布局中将RecyclerView的高度设置为"wrap_content"

make RecyclerView's height to "wrap_content" in Constraint layout

我正在尝试在 wrap_content 上设置回收器视图的高度并使其符合该高度,但它会超过布局上的其他小部件。 我现在可以做什么?

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">



    <TextView
        android:id="@+id/tvPastRounds"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="0dp"
        android:text="Past Rounds"
        android:textColor="@color/text_color_black"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:clipChildren="true"
        android:maxHeight="150dp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/tvPastRounds"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvPastRounds" app:layout_constraintVertical_bias="0.0"/>
</android.support.constraint.ConstraintLayout>

我遇到了同样的问题,我找到了这个解决方案: 您应该将此属性添加到您的 recyclerview,它使您的 recyclerview wrap_content 在 constraint_layout:

app:layout_constraintHeight_default="wrap"

如果此解决方案解决了您的问题,请告诉我。

编辑:回收站的高度应为 0dp。

编辑 2:在较新版本的支持库中,使用此代码:

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_default="wrap"

在较新版本的支持库中已弃用,因此请考虑使用

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"

相反。

我有这个问题。我需要构建一个 UI 页面,其中包含一个 RecyclerView 以及未知数量的应该滚动的元素。该页面具有以下布局:

LinearLayout
    ConstraintLayout
        TextView
        RecyclerView
        TextView

我希望使用 match_parentwrap_content.

程序确定所有高度

我希望 RecyclerView 使用所有可用的 space 介于 两个 TextViews.

关键是给 RecyclerView 一个 "impossible" 约束,方法是:

app:layout_constraintTop_toBottomOf="@+id/topTextView"
app:layout_constraintBottom_toTopOf="@+id/bottomTextView"

并添加:

app:layout_constrainedHeight="true"

如果需要在顶部TextViewRecyclerView之间添加边距(and/or底部TextViewRecyclerView)将边距放在RecyclerView,不是 TextViews.

代码示例

下面是我正在使用的实际代码(为了便于阅读,删除了一些文本样式)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#1A1A1C">

        <TextView
            android:id="@+id/selectcampus_es"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginTop="48dp"
            android:text="Pick a campus."
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_marginTop="24dp"
            android:layout_marginBottom="24dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constrainedHeight="true"
            app:layout_constraintTop_toBottomOf="@+id/selectcampus_es"
            app:layout_constraintBottom_toTopOf="@+id/comingsoon_es" />

        <TextView
            android:id="@+id/comingsoon_es"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="48dp"
            android:text="More campuses coming soon."
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />

    </android.support.constraint.ConstraintLayout>
</LinearLayout>

最后一点,HolderView 也只使用 wrap_content。这也是该代码:

<?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatTextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="15dp"
            android:text="Avondale"
            android:alpha="1"/>
</android.support.constraint.ConstraintLayout>

对于来到这里并发现 none 这些解决方案有效的任何人,请确保您的列表项布局的高度为 wrap_content 或静态 dp。如果他们 match_parent 你会在这个问题上摸不着头脑。

此外,不需要layout_constrainedHeight

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

工作正常..