ただ、すぐには移行できなかったり、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で指定して使えます。
- <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/drawer_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <android.support.v7.widget.Toolbar
- android:id="@+id/action_bar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
- app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
- <FrameLayout
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
- <!-- android:background を忘れずに -->
- <android.support.design.internal.ScrimInsetsFrameLayout
- android:id="@+id/navigation_container"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_gravity="start"
- android:background="#ccc"
- android:fitsSystemWindows="true"
- app:insetForeground="#4000">
- <fragment
- android:id="@+id/navigation_drawer"
- android:name="net.yanzm.navigationdrawersample.NavigationDrawerFragment"
- android:layout_width="@dimen/navigation_drawer_width"
- android:layout_height="match_parent"
- tools:layout="@layout/fragment_navigation_drawer" />
- </android.support.design.internal.ScrimInsetsFrameLayout>
- </android.support.v4.widget.DrawerLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="AppTheme" parent="BaseTheme">
- <item name="android:windowDrawsSystemBarBackgrounds">true</item>
- <item name="android:statusBarColor">@android:color/transparent</item>
- </style>
- </resources>
0 件のコメント:
コメントを投稿