How to change the status bar color in Android?

后端 未结 21 2002
旧时难觅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:39

    Update:

    Lollipop:

    public abstract void setStatusBarColor (int color)
    

    Added in API level 21

    Android Lollipop brought with it the ability to change the color of status bar in your app for a more immersive user experience and in tune with Google’s Material Design Guidelines.

    Here is how you can change the color of the status bar using the new window.setStatusBarColor method introduced in API level 21.

    Changing the color of status bar also requires setting two additional flags on the Window; you need to add the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag and clear the FLAG_TRANSLUCENT_STATUS flag.

    Working Code:

    import android.view.Window;
    

    ...

    Window window = activity.getWindow();
    
    // clear FLAG_TRANSLUCENT_STATUS flag:
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
    // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    
    // finally change the color
    window.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));
    

    Offcial developer reference : setStatusBarColor(int)

    Example :material-design-everywhere

    Chris Banes Blog- appcompat v21: material design for pre-Lollipop devices!

    enter image description here

    The transitionName for the view background will be android:status:background.

提交回复
热议问题