Android Lollipop Set Status Bar Text Color

后端 未结 7 1134
遇见更好的自我
遇见更好的自我 2020-12-14 06:30

I\'m trying to set the status bar text color in Android v21, but I\'m not seeing an API method for it. Here\'s what I have so far for the background

MyActivity.java

相关标签:
7条回答
  • 2020-12-14 07:10

    You can not set the status bar text color by specifying any color explicitly

    But you can try below alternative which is Added in API 23,

    You can use "android:windowLightStatusBar" attribute in two ways

    • "android:windowLightStatusBar" = true, status bar text color will be compatible (grey) when status bar color is light
    • "android:windowLightStatusBar" = false, status bar text color will be compatible (white) when status bar color is dark
    <style name="statusBarStyle" parent="@android:style/Theme.DeviceDefault.Light">
            <item name="android:statusBarColor">@color/status_bar_color</item>
            <item name="android:windowLightStatusBar">false</item>
    </style>
    

    You can check above api in below link - https://developer.android.com/reference/android/R.attr.html#windowLightStatusBar

    0 讨论(0)
  • 2020-12-14 07:10

    My preferred solution until now

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    } else {    
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this, R.color.colorSecondPrimaryColor));
    }
    
    0 讨论(0)
  • 2020-12-14 07:12

    You can also do this at runtime. Here is an example for Mono.Android using the flag SystemUiVisibility. You have to do some bitwise operations to change the flag. Your application must be set to target API 23 or higher to compile with this flag.

    //Android 6.0 introduced the ability to set a light colored text on the status bar
    //MyActivity needs to be changed to your activity
    
    if(Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
    {
        int newUiVisibility = (int)MyActivity.Window.DecorView.SystemUiVisibility;
    
        if(state == StatusBarState.Light)
        {
            //Dark Text to show up on your light status bar
            newUiVisibility |= (int)Android.Views.SystemUiFlags.LightStatusBar;
        }
        else if(state == StatusBarState.Dark)
        {
            //Light Text to show up on your dark status bar
            newUiVisibility &= ~(int)Android.Views.SystemUiFlags.LightStatusBar;
        }
    
        MyActivity.Window.DecorView.SystemUiVisibility = (Android.Views.StatusBarVisibility)newUiVisibility;
    }
    
    public enum StatusBarState
    {
        Light,
        Dark
    }
    
    0 讨论(0)
  • 2020-12-14 07:18

    You can use this line to set a darker text color when you are using the white color for the status bar.

    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
    0 讨论(0)
  • 2020-12-14 07:19

    For Android 6.0 and above You can check Sunita's answer.

    Android 5.0

    we are able to change only status bar color not status bar text color.

    there is no method like

    setStatusBarTextColor(getResources().getColor(R.color.orange));
    

    In short, it's not possible on Android 5.0. Check this answer

    0 讨论(0)
  • 2020-12-14 07:20

    Here's a Java implementation of Gandalf458's answer.

    /** Changes the System Bar Theme. */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public static final void setSystemBarTheme(final Activity pActivity, final boolean pIsDark) {
        // Fetch the current flags.
        final int lFlags = pActivity.getWindow().getDecorView().getSystemUiVisibility();
        // Update the SystemUiVisibility dependening on whether we want a Light or Dark theme.
        pActivity.getWindow().getDecorView().setSystemUiVisibility(pIsDark ? (lFlags & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) : (lFlags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR));
    }
    
    0 讨论(0)
提交回复
热议问题