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

时间:2017-03-13 11:25:42

标签: android android-recyclerview android-constraintlayout

我正在尝试在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>

3 个答案:

答案 0 :(得分:43)

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

app:layout_constraintHeight_default="wrap"

请告诉我此解决方案是否解决了您的问题。

编辑:回收者的身高应为0dp。

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

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

答案 1 :(得分:9)

app:layout_constraintHeight_default="wrap"

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

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

代替。

答案 2 :(得分:0)

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

LinearLayout
    ConstraintLayout
        TextView
        RecyclerView
        TextView

我希望使用match_parentwrap_content来确定所有物体的程序高度。

我希望RecyclerView使用两个TextViews之间的所有可用空间

关键是通过执行以下操作为RecyclerView设置“不可能”约束:

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

并添加以下内容:

app:layout_constrainedHeight="true"

如果您需要在顶部TextViewRecyclerView(和/或底部TextViewRecyclerView之间添加边距,则将边距放在{{1} },而不是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
相关问题