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

ali*_*awi 21 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>
Run Code Online (Sandbox Code Playgroud)

Rez*_*ini 56

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

app:layout_constraintHeight_default="wrap"
Run Code Online (Sandbox Code Playgroud)

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

编辑:回收者的身高应该是0dp.

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

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
Run Code Online (Sandbox Code Playgroud)

  • 这仅适用于在recyclelerview中结合android:layout_height ="0dp"和app:layout_constraintHeight_default ="wrap". (11认同)
  • 如果ViewHolder具有wrap_content高度,则不起作用 (4认同)
  • 我很好奇你们是怎么找到这个解决方案的,它不会写在任何地方.@SebasLG (3认同)
  • @Onregs我还发现,如果RecyclerView项目的高度为wrap_content,则此解决方案不起作用。您找到解决方法了吗? (2认同)

Joh*_*ker 12

app:layout_constraintHeight_default="wrap"
Run Code Online (Sandbox Code Playgroud)

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

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
Run Code Online (Sandbox Code Playgroud)

代替.