2017年4月5日水曜日

DrawerLayout を使った画面で StatusBar の色を動的に変える

結論

DrawerLayout の setStatusBarBackground()setStatusBarBackgroundColor() を使う。


補足

DrawerLayout では android:fitsSystemWindows="true" がセットされている場合、自分で StatusBar 部分を描画します。
  1. public class DrawerLayout extends ViewGroup implements DrawerLayoutImpl {  
  2.     ...  
  3.     private Drawable mStatusBarBackground;  
  4.     ...  
  5.     public DrawerLayout(Context context, AttributeSet attrs, int defStyle) {  
  6.         super(context, attrs, defStyle);  
  7.         ...  
  8.         if (ViewCompat.getFitsSystemWindows(this)) {  
  9.             IMPL.configureApplyInsets(this);  
  10.             mStatusBarBackground = IMPL.getDefaultStatusBarBackground(context);  
  11.         }  
  12.   
  13.         ...  
  14.     }  
  15.   
  16.     ...  
  17.   
  18.     @Override  
  19.     public void onDraw(Canvas c) {  
  20.         super.onDraw(c);  
  21.         if (mDrawStatusBarBackground && mStatusBarBackground != null) {  
  22.             final int inset = IMPL.getTopInset(mLastInsets);  
  23.             if (inset > 0) {  
  24.                 mStatusBarBackground.setBounds(00, getWidth(), inset);  
  25.                 mStatusBarBackground.draw(c);  
  26.             }  
  27.         }  
  28.     }  
  29. }  
そのため Window.setStatusBarColor() で色を変えようとしても意図したようになりません。DrawerLayout のメソッドを使いましょう。