如何在不弃用 Kotlin 的情况下使导航栏和状态栏变亮

How to make the navigation and status bar light without it being deprecated in Kotlin

我正在使用以下代码在 Kotlin 中制作导航栏灯和状态栏灯主题:

window.statusBarColor = ContextCompat.getColor(this@MainActivity, R.color.white)
window.navigationBarColor = ContextCompat.getColor(this@MainActivity, R.color.white)
window.decorView.systemUiVisibility View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

window 为:

val window: Window = this@MainActivity.window

除了 systemUiVisibilitySYSTEM_UI_FLAG_LIGHT_NAVIGATION_BARSYSTEM_UI_FLAG_LIGHT_STATUS_BAR.

已被弃用之外,这非常有效

如果我使用的方法已被弃用,那么新的方法是什么?

引用自documentation

Use WindowInsetsController#APPEARANCE_LIGHT_STATUS_BARS instead

在 activity 中解决此问题的方法是访问 WindowInsetsController,然后使用 setSystemBarsAppearance 方法设置标志。像这样:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
    window.insetsController?.setSystemBarsAppearance(
        WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
        WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
    )
} else {
     @Suppress("DEPRECATION")
     window.decorView.systemUiVisibility =
         View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}