安卓状态栏适配暗黑模式

传统xml

  1. 打开/res/themes/themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.[应用名称]" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

下添加

<item name="android:statusBarColor" tools:targetApi="l">@color/white</item>
<item name="android:windowLightStatusBar">?attr/isLightTheme</item>
<item name="android:navigationBarColor">#ffffff</item>

即可设置白天模式的状态栏的颜色为白色。

也可以使用系统预设的颜色:

<item name="android:statusBarColor" tools:targetApi="l">?attr/isLightTheme</item>
<item name="android:windowLightStatusBar">?attr/isLightTheme</item>
<item name="android:navigationBarColor">#000000</item>
  1. 同理,修改/res/themes/themes.xml(night),可以设置暗黑模式下状态栏的颜色。

Jetpack Compose

  1. 导入依赖
repositories {
    mavenCentral()
}

dependencies {
    implementation "com.google.accompanist:accompanist-systemuicontroller:<version>"
}

依赖最新版本是Maven Central
2. 修改ui/theme/Theme.kt
在你的应用主题Compose函数中最后添加:

//状态栏沉浸,自动适应暗黑模式
val systemUiController = rememberSystemUiController()
systemUiController.setSystemBarsColor(colors.background)
posted @ 2021-04-17 16:34  SoClear  阅读(596)  评论(0编辑  收藏  举报