简体   繁体   中英

Change the Color of Status Bar TEXT and icon in API 30

I want the icons and text on the StatusBar to be black. I have tried in various ways, but obviously it is a problem because API 30 does not respond to the already existing solutions I found on the.net. Can anyone give specific instructions on how to change the color of icons and text . Please note that I am using Theme.AppCompat.DayNight.DarkActionBar I ask for your help.

This is what it should look like:

在此处输入图像描述

My style.xml

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">

    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorPrimaryDark" targetApi="23">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="customBackgroundColor">@android:color/transparent</item>
    <item name="customPrimaryTextColor">#000000</item>

</style>

This code changes the background color of the StatusBar, but I needs to change the color of the text and icons on the StatusBar.

    if (Build.VERSION.SDK_INT >= 21) {
        Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(this.getResources().getColor(R.color.primary_color));
    }

As per documentation: you need to use WindowInsetsController to set setAppearanceLightStatusBars which changes the foreground color of the status bars (icons & text):

new WindowInsetsControllerCompat(getWindow(), 
        getWindow().getDecorView()).setAppearanceLightStatusBars(true); 

UPDATE

No need to the below, also they have deprecated APIs:

if (Build.VERSION.SDK_INT >= 21) {
    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
}

To make the status bar light & icons/text dark on API Level 30:

Method 1: Programmatically:

new WindowInsetsControllerCompat(getWindow(), 
                getWindow().getDecorView()).setAppearanceLightStatusBars(true);
getWindow().setStatusBarColor( ResourcesCompat.getColor(getResources(), R.color.primary_color, null)); // Or Color.WHITE make it white

Method 2: In themes.xml:

add this in your app's theme:

<item name="android:windowLightStatusBar">true</item>
<item name="android:statusBarColor">@color/primary_color</item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM