Android开发中如何实现BottomSheetDialog全屏显示?
要实现BottomSheetDialogFragment全屏显示,可按以下步骤修改代码:
修改Dialog类逻辑
class RegistrationDialog : BottomSheetDialogFragment() { // 可选:搭配自定义主题实现更纯净的全屏效果,不需要可删除该方法 override fun getTheme(): Int = R.style.FullScreenBottomSheetTheme override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.dialog_registration, container, false) } override fun onStart() { super.onStart() val bottomSheetDialog = dialog as? BottomSheetDialog ?: return val bottomSheetView = bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) ?: return // 设置底部表高度为全屏 bottomSheetView.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT // 默认直接展开到最大状态 BottomSheetBehavior.from(bottomSheetView).state = BottomSheetBehavior.STATE_EXPANDED } }
可选:添加全屏主题
在res/values/styles.xml中新增主题配置,可去掉默认圆角、边距等样式:
<style name="FullScreenBottomSheetTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog"> <item name="android:windowIsFloating">false</item> <item name="android:padding">0dp</item> <item name="shapeAppearanceOverlay">@null</item> </style>
注意事项
- 确保你的
R.layout.dialog_registration根布局的高度设置为match_parent - 若不需要完全全屏,可将
layoutParams.height替换为具体的数值,比如屏幕高度的80%做适配 - 禁止下拉关闭可额外添加
BottomSheetBehavior.from(bottomSheetView).isDraggable = false配置
内容的提问来源于stack exchange,提问作者William
相关产品推荐
相关产品推荐

