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

如何实现Android BottomSheetDialogFragment显示在系统导航/手势栏下层

Bottom Sheet显示在Android导航/手势栏下层的必要配置
  • 开启全局边到边显示模式
    在Activity的onCreate方法中执行以下代码,禁用系统默认的窗口适配逻辑:
    WindowCompat.setDecorFitsSystemWindows(window, false)
    
  • 配置导航栏基础样式
    将导航栏设为透明,同时关闭系统自动添加的导航栏对比度遮罩(Android 10及以上):
    window.navigationBarColor = Color.TRANSPARENT
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        window.isNavigationBarContrastEnforced = false
    }
    
  • 调整Bottom Sheet对应布局的适配规则
    1. 承载Bottom Sheet的根布局(如CoordinatorLayout)、Bottom Sheet自身的根布局都设置android:fitsSystemWindows="false"
    2. 给布局设置窗口插入监听器,获取导航栏高度后,给Bottom Sheet的实际内容区域添加等于导航栏高度的底部内边距,避免可交互内容被导航栏遮挡:
    ViewCompat.setOnApplyWindowInsetsListener(bottomSheetRoot) { view, insets ->
        val navBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
        view.setPadding(0, 0, 0, navBarHeight)
        insets
    }
    
  • 适配模态Bottom Sheet的特殊配置
    如果使用的是ModalBottomSheet(模态弹出的Bottom Sheet),需要额外对其自身的Window做相同配置:在onCreateDialog方法中获取Dialog的Window,重复上述边到边开启、导航栏透明的配置步骤。
  • 调整Bottom Sheet的折叠高度
    若设置了默认折叠高度peekHeight,需要将原有高度加上导航栏高度,避免折叠状态下的展示内容被导航栏遮挡。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 08:36:03