项目场景:

Jetpack Compose 实现沉浸式状态栏


实现效果:

在这里插入图片描述

在这里插入图片描述

实现步骤:

第一步:
添加依赖

	implementation "com.google.accompanist:accompanist-insets:0.15.0"
    implementation "com.google.accompanist:accompanist-insets-ui:0.15.0"
    implementation "com.google.accompanist:accompanist-systemuicontroller:0.15.0"

若为gradle.kts中添加依赖则改为:

    implementation("com.google.accompanist:accompanist-insets:0.15.0")
    implementation( "com.google.accompanist:accompanist-insets-ui:0.15.0")
    implementation("com.google.accompanist:accompanist-systemuicontroller:0.15.0")

第二步:
在Activity的onCreate中添加:

window.setDecorFitsSystemWindows(false)

此时已经实现了不再为状态栏与导航栏预留位置(即与页面重叠),但状态栏与导航栏会遮挡。


第二步:
将导航栏设为透明

rememberSystemUiController().setStatusBarColor(Color.Transparent, darkIcons = MaterialTheme.colors.isLight)

若使用Material3则使用:

rememberSystemUiController().setStatusBarColor(Color.Transparent, darkIcons = true)

添加位置如下:
在这里插入图片描述


Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐