You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android中设置Button显示在RecyclerView所有列表项之后

问题原因

你当前的布局存在两个核心问题:

  1. 内层ConstraintLayout、外层RelativeLayout高度均设置为wrap_content,当RecyclerView的列表项总高度超过屏幕高度时,整体布局高度会超出屏幕范围,导致按钮被顶到屏幕外不可见
  2. RecyclerView高度设置为wrap_content,没有做高度上限限制,会无限延伸挤压后续控件的显示空间

方案1:按钮跟随列表滚动,位于所有列表项末尾

符合你要求的「按钮定位在所有列表项之后」的效果:列表项数量少不足一屏时,按钮显示在列表下方;列表项超过一屏时,需要滑动到列表最底部才能看到按钮。
修改后的完整布局代码:

<RelativeLayout 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"
    tools:context=".TestText"
    android:id="@+id/lineartest">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerviewtest"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:listitem="@layout/test"/>

            <Button
                android:id="@+id/verify_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Проверить"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/recyclerviewtest"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</RelativeLayout>

注意:如果你的列表项数量超过20条,更推荐将按钮作为RecyclerView的Footer类型添加,能保留RecyclerView的视图回收特性,性能更优。


方案2:按钮固定在屏幕底部

如果需要按钮始终显示在屏幕底部,不跟随列表滑动,可使用该方案,RecyclerView会自动占满按钮上方的所有空间:
修改后的完整布局代码:

<RelativeLayout 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"
    tools:context=".TestText"
    android:id="@+id/lineartest">

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerviewtest"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/verify_button"
            tools:listitem="@layout/test"/>

        <Button
            android:id="@+id/verify_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Проверить"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>

内容的提问来源于stack exchange,提问作者APTEMKA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 02:06:00