DrawerLayout

侧拉菜单作为常见的导航交互控件,最开始在没有没有android官方控件时,很多时候都是使用开源的SlidingMenu,

  1. 它是一个容器:

    官方介绍
    DrawerLayout acts as a top-level containerfor window content that allows for interactive “drawer” views to be pulled out from the edge of the window.

  2. 主界面和侧滑菜单

    To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent.
    Add drawers aschild views after the main content view and set the layout_gravityappropriately. Drawers commonly use match_parent for height with a fixed width.

抽屉菜单的摆放和布局通过android:layout_gravity属性来控制,可选值为leftrightstartend

和很多容器类布局一样,它本身也继承自ViewGroup,只是在内部实现中会默认将第一个子节点作为内容区,第二个作为抽屉菜单,所以写布局的事后必须牢记

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerlayout" android:layout_width="match_parent" android:layout_height="match_parent">
    <!--主界面-->
    <FrameLayout  android:id="@+id/container_view" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout>
    <!--侧滑界面-->
    <FrameLayout  android:id="@+id/navigation_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" android:background="@color/colorAccent"></FrameLayout>

</android.support.v4.widget.DrawerLayout>

DrawerLayout 和 BACK 键

该功能适用于一种情况:
对于一些使用Navigation Drawer的一级导航应用中(例如 谷嘀美图),应用的主要导航实现在Navigation Drawer中,有时候点击BACK键是想切换不同的分类浏览 而不是退出应用。 这个时候 可以在BACK的时候 打开Drawer,提醒用户可以切换分类,或者其他操作。 然后提示用户 再点击Back一次退出应用。

在Drawer打开的时候点击BACK键,Activity的onBackPressed 函数不会触发。

通过如下函数可以禁用该功能
(详情参android.support.v4.widget.DrawerLayout.DrawerLayout(Context, AttributeSet, int)代码):

mDrawerLayout.setFocusableInTouchMode(false)

DrawerLayout只能实现左右滑动

实现上下滑动的要用到github的开源项目umano/AndroidSlidingUpPanel
DrawerLayout_第1张图片

DrawLayout也可以全屏显示没有ActionBar

你可能感兴趣的:(DrawerLayout)