简体   繁体   English

深色状态栏中的浅色文本在 Android 11 中不起作用

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

I'm changing status bar color and text between fragments/activities.我正在更改片段/活动之间的状态栏颜色和文本。 For black status bar I need white text and viceversa.对于黑色状态栏,我需要白色文本,反之亦然。 Due to api deprecation on Android 11 I am using this:由于 Android 11 上的 api 弃用,我正在使用这个:

Dark status bar with light text:带有浅色文本的深色状态栏:

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

Changing to light status bar with dark text:更改为带有深色文本的浅色状态栏:

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

But the first case (dark status bar with light text) is not working properly because text is still dark.但是第一种情况(带有浅色文本的深色状态栏)无法正常工作,因为文本仍然是深色的。

Did I miss to add something?我错过了添加什么吗?

since SetSystemUIVisibilty was deprecated in API level 30, use WindowInsetsController instead:由于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);

All the best!一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 浅色/深色主题操作栏文字 - Android light/dark theme action bar text Android M Light and Dark status bar programmatically - 如何让它再次变暗? - Android M Light and Dark status bar programmatically - how to make it dark again? 基于图像的亮/暗状态栏 - Light/dark Status Bar based on image behind Ionic 3 Android 应用程序的状态栏文本颜色为深色或黑色 - Ionic 3 Android app's status bar Text color to Dark or Black 在 android 11 及以下版本中设置灯状态栏的代码 android 11 弃用警告不会发生 - Code for setting light status bar in android 11 and below android 11 deprecation warning not going 即使使用 Holo Light 主题,Honeycomb 状态栏仍然保持黑暗 - Honeycomb Status Bar remains dark even with Holo Light theme Android 在深色/浅色主题之间切换不起作用 - Android Switching between Dark/Light themes not working Android Theme.AppCompat.Light with Dark Toolbar(用于浅色文本) - Android Theme.AppCompat.Light with Dark Toolbar (for light text) 如何在Android上的Dark App Bar上使用Light SearchView? - How can I use Light SearchView on Dark App Bar on android? 在 Android 11 (API 30) 中以编程方式更改状态栏文本颜色 - Programmatically Change Status Bar Text Color in Android 11 (API 30)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM