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

Jetpack Compose使用ModalBottomSheetLayout时如何让状态栏透明

Jetpack Compose ModalBottomSheetLayout弹出时状态栏透明设置方案
  • 第一步:配置Activity窗口基础属性
    在MainActivity.onCreate的setContent调用前添加如下代码,允许布局延伸到状态栏区域,支持状态栏透明配置:
// 允许布局绘制到系统栏后方
WindowCompat.setDecorFitsSystemWindows(window, false)
window.apply {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        isStatusBarContrastEnforced = false
    }
    statusBarColor = android.graphics.Color.TRANSPARENT
    // 可根据页面配色调整状态栏图标明暗,true为深色图标,false为浅色
    WindowCompat.getInsetsController(this, decorView).isAppearanceLightStatusBars = true
}
  • 第二步:配置ModalBottomSheetLayout属性
    确保你的ModalBottomSheetLayout配置了正确的修饰符和属性:
val sheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
ModalBottomSheetLayout(
    sheetState = sheetState,
    // 按需设置蒙层颜色,需要全透明背景则设为Color.Transparent
    scrimColor = Color.Black.copy(alpha = 0.3f),
    modifier = Modifier.fillMaxSize()
) {
    // 你的主界面内容
}
  • 可选:动态适配状态栏颜色
    如果你的App默认状态栏不是透明色,可监听BottomSheet的状态动态修改:
val systemUiController = rememberSystemUiController()
LaunchedEffect(sheetState.targetValue) {
    systemUiController.setStatusBarColor(
        color = if (sheetState.isVisible) Color.Transparent else YourDefaultStatusBarColor
    )
}

注意:使用rememberSystemUiController需要先引入accompanist-systemuicontroller依赖,也可以直接调用Activity的window对象修改状态栏属性实现同等效果。
额外排查点:如果设置不生效,检查你的Activity主题中是否配置了固定的android:statusBarColor属性,需要将该属性改为@android:color/transparent避免覆盖代码配置。

内容的提问来源于stack exchange,提问作者ruhi viroja

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 10:54:02