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

如何在Jetpack Compose中隐藏ActionBar并让内容延伸至状态栏区域

问题描述

我想要隐藏默认的ActionBar,因此在Manifest文件中配置了如下代码:

<style name="Theme.MyCompose" parent="Theme.Material.DayNight.NoActionBar">
</style>

<style name="Theme.Material.DayNight.NoActionBar" parent="@android:style/Theme.Material.Light.NoActionBar" />

当前实现效果如下图所示:
当前实现效果
我期望达成的效果如下图所示,即图片内容可以覆盖顶部的白色区域,请问该如何实现?
目标效果

更新1

我尝试了Translucent Status Bar搭配Accompanist Insets支持库的方案后,出现了异常表现:当我按如下方式设置窗口flags时:

window.setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )

虽然已开启Insets适配,但最终效果如下图所示:
开启flag后的效果
如果移除上述窗口flags,Insets适配可以正常工作,但状态栏下方会出现阴影效果:
移除flag后的效果


解决方案

不需要使用FLAG_LAYOUT_NO_LIMITS标记,按以下步骤调整即可实现预期的沉浸式效果:

  1. 调整主题配置
    在你使用的App主题中新增如下属性:
<style name="Theme.MyCompose" parent="Theme.Material.DayNight.NoActionBar">
    <!-- 状态栏设为透明 -->
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <!-- 取消窗口默认背景,消除状态栏下方阴影 -->
    <item name="android:windowBackground">@android:color/transparent</item>
    <!-- 可选:如果需要状态栏图标为深色,适配浅色页面背景 -->
    <item name="android:windowLightStatusBar">true</item>
</style>
  1. 调整Activity窗口配置
    在Activity的onCreate方法中、setContent之前添加如下代码,允许内容延伸到系统栏区域:
WindowCompat.setDecorFitsSystemWindows(window, false)
  1. Compose端适配
    使用Accompanist的SystemUiController组件统一配置系统栏样式,需要延伸到状态栏的内容不需要加statusBarsPadding,需要避开状态栏的内容单独加该修饰符即可:
setContent {
    val systemUiController = rememberSystemUiController()
    SideEffect {
        systemUiController.setStatusBarColor(
            color = Color.Transparent,
            darkIcons = true // 可根据页面背景色动态调整
        )
    }
    // 根布局
    Box(modifier = Modifier.fillMaxSize()) {
        // 图片直接铺满全屏,会自动延伸到状态栏区域
        Image(
            painter = painterResource(R.drawable.your_bg),
            contentDescription = null,
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop
        )
        // 其他需要避开状态栏的内容单独加padding
        Column(
            modifier = Modifier
                .fillMaxSize()
                .statusBarsPadding()
        ) {
            // 页面业务内容
        }
    }
}

内容的提问来源于stack exchange,提问作者mikolaj-jalocha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 20:57:02