簡體   English   中英

深色狀態欄中的淺色文本在 Android 11 中不起作用

[英]Light text in dark status bar is not working in Android 11

我正在更改片段/活動之間的狀態欄顏色和文本。 對於黑色狀態欄,我需要白色文本,反之亦然。 由於 Android 11 上的 api 棄用,我正在使用這個:

帶有淺色文本的深色狀態欄:

Window window = getWindow();
    if (mDefaultStatusBarColor == null) {
        mDefaultStatusBarColor = window.getStatusBarColor();
    }
    window.setStatusBarColor(ContextCompat.getColor(this, R.color.black));

    // Sets status text light
    View decorView = window.getDecorView();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        WindowInsetsController wic = decorView.getWindowInsetsController();
        wic.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

更改為帶有深色文本的淺色狀態欄:

Window window = getWindow();
            // Giving the status bar its default color.
            if (mDefaultStatusBarColor != null) {
                window.setStatusBarColor(mDefaultStatusBarColor);
            }

            // Sets status text dark
            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);
            }

但是第一種情況(帶有淺色文本的深色狀態欄)無法正常工作,因為文本仍然是深色的。

我錯過了添加什么嗎?

由於SetSystemUIVisibilty在 API 級別 30 中已棄用,請改用WindowInsetsController

final WindowInsetsController insetsController = getWindow().getInsetsController();
int systemBarsAppearance = insetsController.getSystemBarsAppearance();
// Sets status text light
insetsController.setSystemBarsAppearance(systemBarsAppearance, ~WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
// Sets status bar dark
getWindow().setStatusBarColor(0xff000000);

一切順利!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM