ConstraintLayout - 如何包裹 RecyclerView

ConstraintLayout - how to wrap over RecyclerView

我有一个 ConstraintLayout,它带有一个 appBar(包含一个工具栏)、两个片段(每个片段都包含一个 RecyclerView)和底部的一个按钮。

我想要做的是将布局的高度包裹在 RecyclerView 的大小之上。正如您在下图中看到的,无论列表的大小如何,每个 Recyclerview 都具有相同的高度。

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/template_grey_light"
    tools:context=".presentation.ui.activity.MainActivity">

    <include
        android:id="@+id/AppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/fragmentTop"
        app:layout_constraintVertical_chainStyle="packed"
        layout="@layout/app_bar" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentTop"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/AppBar"
        app:layout_constraintBottom_toTopOf="@+id/fragmentBottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentBottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/fragmentTop"
        app:layout_constraintBottom_toTopOf="@+id/shuffleButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <Button
        android:id="@+id/shuffleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/template_love_red"
        android:text="Go!"
        android:layout_marginBottom="20dp"
        app:layout_constraintTop_toBottomOf="@+id/fragmentBottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我知道我在高度属性上设置了 0dp,这使它的高度相等,但如果我不这样做,所有内容都会在顶部重叠。知道如何让它发挥作用吗?

在您的 FragmentContainerView 标签中使用 android:layout_height="wrap_content"