【问题标题】:Programmatically Change Status Bar Text Color in Android 11 (API 30)在 Android 11 (API 30) 中以编程方式更改状态栏文本颜色
【发布时间】:2021-12-12 11:07:57
【问题描述】:

我目前可以在我的基本活动中使用以下内容将状态栏 文本 颜色从浅色更新为深色:

private fun toggleStatusBarTextColor(light: Boolean) {
    // clear any existing flags
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
    if(light) {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    } else {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
    }
}

systemUiVisibility 现在在 API 30 上显示为已弃用,尽管已弃用的方法暂时仍会起作用,但我更愿意用更新的方法替换它们来完成此操作。我已经读到我们现在应该使用 WindowInsetsController 函数,但不清楚如何从文档中完成此操作。有人能指出我正确的方向吗?

【问题讨论】:

    标签: java android kotlin android-theme android-statusbar


    【解决方案1】:

    对于 API 30,您可以使用 WindowInsetsController.setSystemBarsAppearance (int appearance, int mask):

    使状态栏变亮:

    window.insetsController?.setSystemBarsAppearance(
            WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
            WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
    )
    

    清除标志:

    window.insetsController?.setSystemBarsAppearance(
            0,
            WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
    )
    

    请注意,getInsetsController 可以为空,因此需要检查 ?

    您也可以使用WindowInsetControllerCompat

    val windowInsetController = ViewCompat.getWindowInsetsController(window.decorView)
    windowInsetController?.isAppearanceLightStatusBars = true // or false
    

    注意:如果清除标志不起作用,请检查 window.decorView.windowSystemUiVisibility 的值 - 如果它包含 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR,这意味着您的视图层次结构包含一个带有此标志的视图,该标志被传播并影响 systemUiVisibility 计算。

    【讨论】:

    • 无法将文本设置为白色
    【解决方案2】:

    我和其他人一样,无法获得@Pawel 推荐的适用于所有 Android 操作系统版本的新 API。不幸的是,我发现我必须同时使用旧 API 和新 API 才能使其在 Android 11 及更低版本上运行:

    fun setStatusBarLightText(window: Window, isLight: Boolean) {
        setStatusBarLightTextOldApi(window, isLight)
        setStatusBarLightTextNewApi(window, isLight)
    }
    
    
    private fun setStatusBarLightTextOldApi(window: Window, isLight: Boolean) {
        val decorView = window.decorView
        decorView.systemUiVisibility =
            if (isLight) {
                decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
            } else {
                decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
    }
    
    private fun setStatusBarLightTextNewApi(window: Window, isLightText: Boolean) {
        ViewCompat.getWindowInsetsController(window.decorView)?.apply {
            // Light text == dark status bar
            isAppearanceLightStatusBars = !isLightText
        }
    }
    

    【讨论】:

    • setStatusBarLightTextNewApi 帮助我用灰色图标将状态栏设为白色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多