How to change the status bar color in Android?

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

    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

    
    

    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:

    
    
    
    
    //The rest of your layout       
    
    

    For the Toolbar, add top margin attribute:

    
    

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

    styles-v19.xml, v21:

    
    true
    
    

    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:

    
    4dp
    0dp
    0dp
    
    

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

    
    24dp
    24dp
    
    

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

    
    4dp
    
    

    This is the result for Jellybean KitKat and Lollipop:

    enter image description here

提交回复
热议问题