将Navigation Drawer显示在Toolbar的前面,Statusbar的后面,并且Statusbar为semi-transparent的样子。
在项目中加入ScrimInsetsFrameLayout.java:
并在Drawer Layout的RecyclerView外加上ScrimInsetsFrameLayout:
activity_main.xml
<!-- The navigation drawer --> <com.whisper.myapplication.ScrimInsetsFrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" android:elevation="8dp" app:insetForeground="#4000" > <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" android:background="#FAFAFA" android:scrollbars="vertical" /> </com.whisper.myapplication.ScrimInsetsFrameLayout> </android.support.v4.widget.DrawerLayout>
themes.xml(v21)
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.AppTheme" parent="@style/AppBaseTheme"> <!-- 顶部状态栏颜色 --> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> <item name="android:windowContentTransitions">true</item> <item name="android:windowAllowEnterTransitionOverlap">true</item> <item name="android:windowAllowReturnTransitionOverlap">true</item> <item name="android:windowSharedElementEnterTransition">@android:transition/move</item> <item name="android:windowSharedElementExitTransition">@android:transition/move</item> <!-- 底部导航栏颜色 --> <item name="android:navigationBarColor">#0288D1</item> </style> </resources>
增加一个为ScrimInsetsFrameLayout服务的attrs.xml
<resources> <declare-styleable name="ScrimInsetsView"> <attr format="reference|color" name="insetForeground"> </attr></declare-styleable> </resources>
参考文章:
NAVIGATION DRAWER WITH TRANSPARENT STATUS BAR Navigation Drawer semi-transparent over status bar not working 注:一开始我将xml文件写成如下的样式:<!-- The navigation drawer --> <com.whisper.myapplication.ScrimInsetsFrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" android:elevation="8dp" app:insetForeground="#4000" > <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" android:background="#FAFAFA" android:scrollbars="vertical" > </android.support.v7.widget.RecyclerView> </com.whisper.myapplication.ScrimInsetsFrameLayout> </android.support.v4.widget.DrawerLayout>