如何在Fragment中让Floating Action Button覆盖RecyclerView(无需置于MainActivity)
解决Fragment中FAB覆盖RecyclerView的布局问题
嘿,这问题我之前也踩过坑!垂直LinearLayout是线性排列子元素的,你的FAB会乖乖排在SwipeRefreshLayout的下方,自然没法覆盖在RecyclerView上面。要实现层叠效果,你需要用支持视图堆叠的布局容器,比如FrameLayout或者ConstraintLayout,这俩都能在Fragment里直接用,完全不用动MainActivity。
方案一:用FrameLayout实现(最简单)
把根布局换成FrameLayout,让SwipeRefreshLayout占满整个屏幕,然后把FAB放在后面,通过layout_gravity控制它的位置,这样它就会自动覆盖在RecyclerView上方了:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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=".ui.filing.timelog.TimelogFilingFragment"> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/pullFilingTimelog" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:overScrollMode="never" android:id="@+id/rvFilingTimelog" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/floating_action_button" style="@style/Widget.Design.FloatingActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:backgroundTint="@color/borderColor" android:layout_margin="16dp" app:tint="#FFFFFF" app:srcCompat="@drawable/ic_add"/> </FrameLayout>
关键说明:
FrameLayout的子视图是按代码顺序堆叠的,后添加的视图会显示在前面,所以一定要把FAB写在SwipeRefreshLayout的后面layout_gravity="bottom|center"保持了你原来想要的居中底部位置,也可以改成bottom|end让它靠右(更符合Material Design规范)
方案二:用ConstraintLayout实现(更灵活)
如果你需要更复杂的布局约束,ConstraintLayout是更好的选择,同样能轻松实现覆盖效果:
<?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" tools:context=".ui.filing.timelog.TimelogFilingFragment"> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/pullFilingTimelog" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <androidx.recyclerview.widget.RecyclerView android:overScrollMode="never" android:id="@+id/rvFilingTimelog" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/floating_action_button" style="@style/Widget.Design.FloatingActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="@color/borderColor" android:layout_margin="16dp" app:tint="#FFFFFF" app:srcCompat="@drawable/ic_add" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"/> </FrameLayout>
关键说明:
- 通过
layout_constraintBottom_toBottomOf="parent"和layout_constraintStart_toStartOf="parent"+layout_constraintEnd_toEndOf="parent"实现居中底部的位置 - ConstraintLayout默认也是后添加的视图层级更高,所以FAB会显示在RecyclerView上方
两种方案都能完美实现你要的效果,选哪种看你后续布局的复杂度需求~
内容的提问来源于stack exchange,提问作者Marlon
相关产品推荐
相关产品推荐

