本文翻译自:How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?
I've seen in the new material design Side Nav spec that you can display the drawer over the action bar and behind the status bar. 我在新的材质设计Side Nav规范中看到,可以在操作栏上方和状态栏后面显示抽屉。 How can I implement this? 我该如何实施?
#1楼
参考:https://stackoom.com/question/1mwTn/如何使用DrawerLayout在操作栏-工具栏上方和状态栏下方显示
#2楼
New functionality in the framework and support libs allow exactly this. 框架和支持库中的新功能完全可以做到这一点。 There are three 'pieces of the puzzle': 一共有三个“谜题”:
- Using Toolbar so that you can embed your action bar into your view hierarchy. 使用工具栏,以便您可以将操作栏嵌入到视图层次结构中。
- Making DrawerLayout
fitsSystemWindows
so that it is layed out behind the system bars. 使DrawerLayout适合fitsSystemWindows
以便将其fitsSystemWindows
在系统栏后面。 - Disabling
Theme.Material
's normal status bar coloring so that DrawerLayout can draw there instead. 禁用Theme.Material
的正常状态栏着色,以便DrawerLayout可以在此处绘制。
I'll assume that you will use the new appcompat. 我假设您将使用新的appcompat。
First, your layout should look like this: 首先,您的布局应如下所示:
<!-- The important thing to note here is the added fitSystemWindows -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- The rest of your content view -->
</LinearLayout>
<!-- Your drawer view. This can be any view, LinearLayout
is just an example. As we have set fitSystemWindows=true
this will be displayed under the status bar. -->
<LinearLayout
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:fitsSystemWindows="true">
<!-- Your drawer content -->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>