项目场景:
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)
添加位置如下: