首页 > 解决方案 > RecyclerView 不会填满剩余空间(ConstraintLayout)

问题描述

我试图让我的 RecyclerView 填补两个视图之间的空白,一个位于布局的顶部,另一个位于底部。我尝试将 RecyclerView 的高度设置为 0dp 并将约束设置在下方,但由于某种原因,RecyclerView 根本没有呈现。适配器实际上确实包含数据,如果我将 RecyclerView 的高度设置为任意值,例如 200dp,那么它的行会按预期显示。

app:layout_constraintTop_toBottomOf="@id/label_realm_filter"
app:layout_constraintBottom_toTopOf="@id/btn_realm_filter_apply"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

这是我的其余布局:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:background="@drawable/bg_white_rounded_edges"
    app:cardCornerRadius="6dp"
    app:cardElevation="6dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/label_realm_filter"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginRight="8dp"
            android:text="@string/realm"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/btn_realm_filter_close"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/btn_realm_filter_close"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginRight="24dp"
            android:src="@drawable/ic_close"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list_realm_filter"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="18dp"
            android:layout_marginBottom="18dp"
            app:layout_constraintTop_toBottomOf="@id/label_realm_filter"
            app:layout_constraintBottom_toTopOf="@id/btn_realm_filter_apply"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />

        <TextView
            android:id="@+id/btn_realm_filter_apply"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="24dp"
            android:layout_marginBottom="24dp"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="true"
            android:gravity="right"
            android:text="@string/apply"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

感谢您的时间。

标签: androidandroid-layoutandroid-constraintlayout

解决方案


推荐阅读