You need to enable JavaScript to run this app. Android开发中如何实现BottomSheetDialog全屏显示?
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 08:27:11