android - SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 和 FLAG_TRANSLUCENT_STATUS 已弃用

标签 android kotlin

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);

关于android - SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 和 FLAG_TRANSLUCENT_STATUS 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65423778/

相关文章:

android - C & Android 内核模块 : what happened with f_flags here?

android - WorkManager alpha10 ListenableFuture 用法

Android R.java的使用

java - 我无法在 SQLite 中按 id 删除某些行

android - 通过复选框激活android中的停用音乐

android - 如何在android中调用Restful web服务

intellij-idea - 生成 Kotlin 方法/类注释

android - Distinct 不适用于 Kotlin 中的 ArrayList

debugging - 如何调试在IntelliJ的Kubernetes容器中运行的Kotlin应用程序?

java - 使用fragmenttransaction添加嵌套fragment时无法唯一标识布局