How to change the status bar color in Android?

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

    Android 5.0 Lollipop introduced Material Design theme which automatically colors the status bar based on the colorPrimaryDark value of the theme.

    This is supported on device pre-lollipop thanks to the library support-v7-appcompat starting from version 21. Blogpost about support appcompat v21 from Chris Banes

    enter image description here

    Read more about the Material Theme on the official Android Developers website

    0 讨论(0)
  • 2020-11-21 16:27

    Well, Izhar solution was OK but, personally, I am trying to avoid from code that looks as this:

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    //Do what you need for this SDK
    };
    

    As well, I don't like to duplicate code either. In your answer I have to add such line of code in all Activities:

    setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));
    

    So, I took Izhar solution and used XML to get the same result: Create a layout for the StatusBar status_bar.xml

    <View xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="@dimen/statusBarHeight"
         android:background="@color/primaryColorDark"
         android:elevation="@dimen/statusBarElevation">
    

    Notice the height and elevation attributes, these will be set in values, values-v19, values-v21 further down.

    Add this layout to your activities layout using include, main_activity.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Black" >
    
    <include layout="@layout/status_bar"/>
    <include android:id="@+id/app_bar" layout="@layout/app_bar"/>
    //The rest of your layout       
    </RelativeLayout>
    

    For the Toolbar, add top margin attribute:

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@color/primaryColor"
    app:theme="@style/MyCustomToolBarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="@dimen/toolbarElevation"
    android:layout_marginTop="@dimen/appBarTopMargin"
    android:textDirection="ltr"
    android:layoutDirection="ltr">
    

    In your appTheme style-v19.xml and styles-v21.xml, add the windowTranslucent attr:

    styles-v19.xml, v21:

    <resources>
    <item name="android:windowTranslucentStatus">true</item>
    </resources>
    

    And finally, on your dimens, dimens-v19, dimens-v21, add the values for the Toolbar topMargin, and the height of the statusBarHeight: dimens.xml for less than KitKat:

    <resources>
    <dimen name="toolbarElevation">4dp</dimen>
    <dimen name="appBarTopMargin">0dp</dimen>
    <dimen name="statusBarHeight">0dp</dimen>
    </resources>
    

    The status bar height is always 24dp dimens-v19.xml for KitKat and above:

    <resources>
    <dimen name="statusBarHeight">24dp</dimen>
    <dimen name="appBarTopMargin">24dp</dimen>
    </resources>
    

    dimens-v21.xml for Lolipop, just add the elevation if needed:

    <resources>
    <dimen name="statusBarElevation">4dp</dimen>
    </resources>
    

    This is the result for Jellybean KitKat and Lollipop:

    enter image description here

    0 讨论(0)
  • 2020-11-21 16:29

    I had this requirement: Changing programmatically the status bar color keeping it transparent, to allow the Navigation Drawer to draw itself overlapping the trasparent status bar.

    I cannot do that using the API

    getWindow().setStatusBarColor(ContextCompat.getColor(activity ,R.color.my_statusbar_color)
    

    If you check here in stack overflow everyone before that line of code set the transparency of the status bar to solid with

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    

    I'm able to manage color and transparency of status bar like this:

    • Android 4: there's not much you can do, because you can't manage status bar color from the API ... the only thing you can do is to set the status bar as translucent and move a colored element of you UI under the status bar. To do this you need to play with

      android:fitsSystemWindows="false"
      

      in your main layout. This allows you to draw you layout under the status bar. Then you need to play with some padding with the top of your main layout.

    • Android 5 and above: you have to define a style with

      <item name="android:windowDrawsSystemBarBackgrounds">true</item>
      <item name="android:statusBarColor">@android:color/transparent</item>
      

      this allows the navigation drawer to overlap the status bar.

      Then to change the color keeping the status bar transparent you have to set the status bar color with

      drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(activity, R.color.my_statusbar_color))
      

      where drawerLayout is defined like this

      <android.support.v4.widget.DrawerLayout
          android:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true">
      
    0 讨论(0)
  • 2020-11-21 16:30

    Edit the colorPrimary in the colors.xml in Values to the color you want the Status Bar to be. For example:

       <resources>
    <color name="colorPrimary">#800000</color> // changes the status bar color to Burgundy
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="red">#FF0000</color>
    <color name="white">#FFFFFF</color>
    <color name="cream">#fffdd0</color>
    <color name="burgundy">#800000</color>
    

    0 讨论(0)
  • 2020-11-21 16:31

    This is what worked for me in KitKat and with good results.

    public static void setTaskBarColored(Activity context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                Window w = context.getWindow();
                w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //status bar height
                int statusBarHeight = Utilities.getStatusBarHeight(context);
    
                View view = new View(context);
                view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                view.getLayoutParams().height = statusBarHeight;
                ((ViewGroup) w.getDecorView()).addView(view);
                view.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryTaskBar));
            }
        }
    
    0 讨论(0)
  • 2020-11-21 16:33

    You can change the status bar color with this function. works on android L means API 21 and higher and needs a color string such as "#ffffff".

    private void changeStatusBarColor(String color){
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor(color));
        }
    }
    
    0 讨论(0)
提交回复
热议问题