如何让Bottom Sheet关闭时不完全隐藏,保留顶部可拉起区域?
实现底部弹窗(Bottom Sheet)关闭时保留顶部可见的方案
完全可以实现这个需求,只需要调整Bottom Sheet Behavior的几个核心属性就能搞定:
- 修改布局属性
- 将
app:behavior_hideable="true"改为app:behavior_hideable="false",这个属性控制Bottom Sheet是否允许被完全隐藏,设为false后弹窗就不会彻底消失。 - 添加
app:behavior_peekHeight="60dp"(数值可根据你的实际需求调整),这个属性定义了Bottom Sheet收起状态下保留的可见高度,设置后收起时会固定显示该高度的区域,方便用户重新拉起。
- 将
修改后的布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <com.app.implementation.bottomsheet.BrowserBottomSheet 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="wrap_content" android:clickable="true" android:focusable="true" app:behavior_hideable="false" app:behavior_peekHeight="60dp" app:layout_behavior="@string/bottom_sheet_behavior" />
- 动态调整(可选)
如果需要在代码中动态修改保留高度,可以通过获取BottomSheetBehavior实例操作:
// Kotlin示例 val bottomSheet = findViewById<BrowserBottomSheet>(R.id.your_bottom_sheet_id) val behavior = BottomSheetBehavior.from(bottomSheet) behavior.peekHeight = 180 // 单位为像素 behavior.isHideable = false
注意:peekHeight的数值要匹配你顶部可交互区域的大小,确保用户能轻松点击或拖拽拉起弹窗。
内容的提问来源于stack exchange,提问作者sdds
相关产品推荐
相关产品推荐

