RecyclerView 在 ConstraintLayout 中将其下方按钮推出屏幕

ste*_*ris 1 xml android android-layout android-recyclerview

我正在尝试使用固定在屏幕顶部的“X”按钮来设置布局,然后在视图中居中放置两个元素。一个回收器视图,然后固定在回收器视图下方,一个用于表单提交的按钮。我目前工作的布局,直到回收站视图超出其边界。然后提交按钮被推到视图边界下方,回收器视图不会留在布局内。如果回收器视图变大,如何将两个回收器视图和按钮视图居中,但不让按钮越过视图边界?

View With Small Recycler View 显示为(它应该居中。我的例子有点偏离。)

在此处输入图片说明

视图应该如何与更大的回收器视图一起出现(回收器视图的内容太大而无法滚动)

在此处输入图片说明

视图实际上如何在更大的回收器视图中出现(回收器视图确实滚动,但现在它将按钮推离视图底部,这显示为按钮被切断)

在此处输入图片说明

XML 布局的相关代码块

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.45"
        android:orientation="vertical"
        android:background="@color/backgroundLightSecondary"
        android:padding="20dp" >

        <Button
            android:id="@+id/bt_close"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="end"
            android:background="@drawable/ic_close"
            android:textColor="@color/textLightPrimary"  />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center_vertical">

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

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_item_options"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <Button
                android:id="@+id/bt_order"
                android:layout_width="150dp"
                android:layout_height="50dp"
                android:layout_weight="0"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:background="@drawable/bt_rounded_corner"
                android:fontFamily="@font/microbrew_two"
                android:padding="3dp"
                android:text="@string/btn_add_to_order"
                android:textColor="@color/backgroundLightSecondary"
                android:textSize="20sp" />

        </LinearLayout>

    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ami*_*phy 6

在这种情况下,最好的方法是使用 aConstraintLayout作为容器。正如你在问题中提到的,你想把 放在RecyclerView中心。因此,它导致将其顶部和底部绑定到按钮。

另一方面,如果我们在RecyclerView和之间建立一个链,submitButton垂直链样式为packed,则submitButton将粘在 的底部RecyclerView(高度wrap_content可以增长)并随着底部移动,直到到达屏幕底部(因为app:layout_constraintBottom_toBottomOf="parent")。

关键的一点是设置app:layout_constrainedHeight="true"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:padding="8dp" >

    <androidx.appcompat.widget.AppCompatImageButton
        android:id="@+id/closeImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_baseline_close_24"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@id/submitButton"
        app:layout_constraintTop_toBottomOf="@id/closeImageButton"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recyclerView" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

视觉效果: