什么是DrawerLayout
官方文档是这样说的
The navigation drawer is a panel that displays the app’s main navigation options on the left edge of the screen. It is hidden most of the time, but is revealed when the user swipes a finger from the left edge of the screen or, while at the top level of the app, the user touches the app icon in the action bar.
他是在屏幕的左侧显示应用的主要导航选项的一个面板。在大多数情况下它是隐藏的,但是当用户用户从屏幕左边滑动时或者当用户点击用用顶部的ActionBar的图标时,它就显示出来了。(翻译略烂)
添加DrawerLayout
To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
为了添加一个导航抽屉,应该在你的布局文件的根部声明一个DrawerLayout,在这个DrawerLayout里面,添加两个View,一个View包含了当抽屉隐藏的时候屏幕显示的主要内容。另一个View包含了导航抽屉的主要内容。
For example, the following layout uses a DrawerLayout with two child views: a FrameLayout to contain the main content (populated by a Fragment at runtime), and a ListView for the navigation drawer.
例如,下面这个布局DrawerLayout是根布局,下面包含两个子View,一个FrameLayout包含主要内容(运行时被Fragment填充),一个ListView是显示导航抽屉的列表。
**<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>**
注意事项
1、This layout demonstrates some important layout characteristics:
2、The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content.
3、The main content view is set to match the parent view’s width and height, because it represents the entire UI when the navigation drawer is hidden.
4、The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravity attribute. To support right-to-left (RTL) languages, specify the value with “start” instead of “left” (so the drawer appears on the right when the layout is RTL).
5、The drawer view specifies its width in dp units and the height matches the parent view. The drawer width should be no more than 320dp so the user can always see a portion of the main content.
1、主要内容的布局(这里是FrameLayout)必须是DrawerLayout的第一个子布局
2、主要内容的布局宽度和高度都必须设置为march_parent,这是因为当抽屉隐藏时,它显示的是整个UI。
3、抽屉布局必须包含layout_gravity属性,当属性值为start时,表示从左向右划出,属性值为end时,表示从右向左滑出。
4、抽屉布局应该指定宽度使用dp单位,高度设为match_parent,宽度不应该超过320dp,这样的话即时抽屉显示出来了,用户也能看见一部分主要界面的内容。
到这里就可以创建一个DrawerLayout了。
使用实例(与ToolBar结合)
主界面布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent