2015年6月13日土曜日

ScrimInsetsFrameLayout を使うときは android:background を指定する

Android Design Support Library で NavigationView が用意されましたね。
ただ、すぐには移行できなかったり、NavigationView では今のものを置き換えられれない場合などもあるでしょう。

そうは言っても StatusBar 部分の処理だけでも取り込みたい、という場合 ScrimInsetsFrameLayout で包むという方法が使えます。

参考: http://stackoverflow.com/questions/26745300/navigation-drawer-semi-transparent-over-status-bar-not-working

ScrimInsetsFrameLayout はもともと Google I/O アプリで使われていたクラスで、android:fitsSystemWindows="true" で計算される領域と View の領域との差分領域を app:insetForeground で指定された色で塗るという処理をしています。

ScrimInsetsFrameLayout は FrameLayout を継承しているので ViewGroup です。ViewGroup は常に draw() が呼ばれるわけではなく、描画の必要があるときしか呼ばれません。そのため、ScrimInsetsFrameLayout に直接背景を指定しないと draw() が呼ばれず StatusBar 部分に app:insetForeground で指定した色が塗られません。

ScrimInsetsFrameLayout を継承している NavigationView ではコンストラクタで setBackgroundDrawable() を呼んでいます。

Design Support Library の ScrimInsetsFrameLayout は @hi de ですが、こんな感じでXMLで指定して使えます。
  1. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/drawer_layout"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:fitsSystemWindows="true">  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:orientation="vertical">  
  13.   
  14.         <android.support.v7.widget.Toolbar  
  15.             android:id="@+id/action_bar"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="?attr/actionBarSize"  
  18.             android:background="?attr/colorPrimary"  
  19.             android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"  
  20.             app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />  
  21.   
  22.         <FrameLayout  
  23.             android:id="@+id/container"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="match_parent" />  
  26.   
  27.     </LinearLayout>  
  28.   
  29.     <!--  android:background を忘れずに -->  
  30.     <android.support.design.internal.ScrimInsetsFrameLayout  
  31.         android:id="@+id/navigation_container"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="match_parent"  
  34.         android:layout_gravity="start"  
  35.         android:background="#ccc"  
  36.         android:fitsSystemWindows="true"  
  37.         app:insetForeground="#4000">  
  38.   
  39.         <fragment  
  40.             android:id="@+id/navigation_drawer"  
  41.             android:name="net.yanzm.navigationdrawersample.NavigationDrawerFragment"  
  42.             android:layout_width="@dimen/navigation_drawer_width"  
  43.             android:layout_height="match_parent"  
  44.             tools:layout="@layout/fragment_navigation_drawer" />  
  45.   
  46.     </android.support.design.internal.ScrimInsetsFrameLayout>  
  47.   
  48. </android.support.v4.widget.DrawerLayout>  
values-v21/styles.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="AppTheme" parent="BaseTheme">  
  4.         <item name="android:windowDrawsSystemBarBackgrounds">true</item>  
  5.         <item name="android:statusBarColor">@android:color/transparent</item>  
  6.     </style>  
  7. </resources>  



0 件のコメント:

コメントを投稿