在Constraint布局中使RecyclerView的高度为"wrap_content"

41

我想将Recycler View的高度设置为wrap_content并使其遵循此设置,但它会超出布局中的其他小部件。现在我该怎么办?


我尝试设置recycler view的高度为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>
4个回答

118

我遇到了同样的问题,然后找到了这个解决方案: 您应该将此属性添加到您的RecyclerView中,并且它会使您的RecyclerView在ConstraintLayout中具有wrap_content的效果:

我遇到了同样的问题,然后找到了这个解决方案: 您应该将此属性添加到您的RecyclerView中,并且它会使您的RecyclerView在ConstraintLayout中具有wrap_content的效果:

app:layout_constraintHeight_default="wrap"

如果这个解决方案解决了你的问题,请告诉我。

编辑:recycler的高度应该是0dp。

编辑2:在较新版本的support库中,请使用以下代码:

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

12
仅当在RecyclerView中同时使用android:layout_height="0dp"和app:layout_constraintHeight_default="wrap"时,此方法才有效。 - Sebas LG
6
我只是好奇,你们是如何找到这个解决方案的,因为它没有写在任何地方。@SebasLG - Ultimo_m
2
@Onregs,我也发现如果RecyclerView项的高度为wrap_content,这个解决方案不起作用。你有找到任何解决办法吗? - David
有人知道为什么RecyclerView需要单独的逻辑吗?这个对我来说有效,但是让我想了一下,为什么RecyclerView需要这个属性才能在ConstraintLayout中正常工作? - capt.swag
如果有人遇到类似水平方向的问题,则按照上述步骤处理宽度。例如,app:layout_constraintWidth_default="wrap" app:layout_constrainedWidth="true" android:layout_width="wrap_content" - Hitesh Sarsava

21
app:layout_constraintHeight_default="wrap"

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

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

相反。


1

如果有人来到这里,发现这些解决方案都不起作用,请确保您的列表项布局高度为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"/>

正常运行...

1

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

LinearLayout
    ConstraintLayout
        TextView
        RecyclerView
        TextView

我希望所有内容都能使用match_parentwrap_content来进行程序化确定高度。
我希望RecyclerView可以使用两个TextView之间的所有可用空间。
关键在于给RecyclerView一个“不可能”的约束条件,方法如下:
app:layout_constraintTop_toBottomOf="@+id/topTextView"
app:layout_constraintBottom_toTopOf="@+id/bottomTextView"

并添加这个:

app:layout_constrainedHeight="true"

如果您需要在顶部TextViewRecyclerView(和/或底部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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接