How to change the status bar color in Android?

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

      true
      @android:color/transparent
      

      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

      
      

提交回复
热议问题