SYSTEM_UI_FLAG_LIGHT_STATUS_BAR和FLAG_TRANSLUCENT_STATUS已弃用

此代码在 API 30 中已弃用。知道如何更新此代码吗?

private fun setSystemBarLight(act: Activity) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
           val view: View = act.findViewById(android.R.id.content)
           var flags: Int = view.systemUiVisibility
           flags = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
           view.systemUiVisibility = flags
      }
}

FLAG_TRANSLUCENT_STATUS此处也已弃用。我需要帮助修复此警告。

    private fun setSystemBarColor(act: Activity, color: String?) {
                val window: Window = act.window
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                window.statusBarColor = Color.parseColor(color)
            }

回答

这些解决方案仅适用于 API > 23。

更新:

正如评论部分所建议的,您可以按如下方式使用WindowInsetsControllerCompat

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    Window window = getWindow();
    View decorView = window.getDecorView();

    WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, decorView);

    wic.setAppearanceLightStatusBars(true); // true or false as desired.

    // And then you can set any background color to the status bar.
    window.setStatusBarColor(Color.WHITE);
}

旧答案:

在 API 级别 30 setSystemUiVisibility()已被弃用。因此你应该使用WindowInsetsController。下面的代码将使状态栏亮起,这意味着状态栏中的文本将为黑色。

Window window = getWindow();
View decorView = window.getDecorView();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    WindowInsetsController wic = decorView.getWindowInsetsController();
    wic.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

// set any light background color to the status bar. e.g. - white or light blue
window.setStatusBarColor(Color.WHITE);

要清除这一点,请使用

wic.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS);

  • Now this answer is outdated as well, as one should use `WindowInsetsControllerCompat(window, window.decorView)` and `wic.isAppearanceLightStatusBars`.

以上是SYSTEM_UI_FLAG_LIGHT_STATUS_BAR和FLAG_TRANSLUCENT_STATUS已弃用的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>