How to change the status bar color in Android?

后端 未结 21 1890
旧时难觅i
旧时难觅i 2020-11-21 16:09

First of all it\'s not a duplicate as in How to change the background color of android status bar

How do I change the status bar color which should be same as in nav

相关标签:
21条回答
  • 2020-11-21 16:19

    this is very easy way to do this without any Library: if the OS version is not supported - under kitkat - so nothing happend. i do this steps:

    1. in my xml i added to the top this View:
    <View
            android:id="@+id/statusBarBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    

    then i made this method:

     public void setStatusBarColor(View statusBar,int color){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
               Window w = getWindow();
               w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
               //status bar height
               int actionBarHeight = getActionBarHeight();
               int statusBarHeight = getStatusBarHeight();
               //action bar height
               statusBar.getLayoutParams().height = actionBarHeight + statusBarHeight;
               statusBar.setBackgroundColor(color);
         }
    }
    

    also you need those both methods to get action Bar & status bar height:

    public int getActionBarHeight() {
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        {
           actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
        }
        return actionBarHeight;
    }
    
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    

    then the only thing you need is this line to set status bar color:

    setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));
    
    0 讨论(0)
  • 2020-11-21 16:19

    For Java Developers:

    As @Niels said you have to place in values-v21/styles.xml:

    <item name="android:statusBarColor">@color/black</item>
    

    But add tools:targetApi="lollipop" if you want single styles.xml, like:

    <item name="android:statusBarColor" tools:targetApi="lollipop">@color/black</item>
    

    For Kotlin Developers:

    window.statusBarColor = ContextCompat.getColor(this, R.color.color_name)
    
    0 讨论(0)
  • 2020-11-21 16:20

    Just create a new theme in res/values/styles.xml where you change the "colorPrimaryDark" which is the color of the status bar:

    <style name="AppTheme.GrayStatusBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimaryDark">@color/colorGray</item>
    </style>
    

    And modify the activity theme in AndroidManifest.xml to the one you want, on the next activity you can change the color back to the original one by selecting the original theme:

    <activity
        android:name=".LoginActivity"
        android:theme="@style/AppTheme.GrayStatusBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    This is how your res/values/colors.xml should look like:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#c6d6f0</color>
        <color name="colorGray">#757575</color>
    </resources>
    
    0 讨论(0)
  • 2020-11-21 16:23

    i used this code to change status bar to transparent

        activity?.window?.setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
    

    to change it to color in style used this code i used in fragment in onDetach()

    activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
    
    0 讨论(0)
  • 2020-11-21 16:25

    Solution is very Simple, put the following lines into your style.xml

    For dark mode:

    <item name="android:windowLightStatusBar">false</item>
    <item name="android:statusBarColor">@color/black</item>
    
    0 讨论(0)
  • 2020-11-21 16:26

    You can use this simple code:

    One-liner in Kotlin:

    window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)
    

    Original answer with Java & manual version check:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light, this.getTheme()));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light));
    }
    
    0 讨论(0)
提交回复
热议问题