Android: DrawerLayout 侧滑菜单栏

DrawerLayout是SupportLibrary包中实现的侧滑菜单效果的控件。

分为主内容区域和侧边菜单区域

drawerLayout本身就支持:侧边菜单根据手势展开与隐藏,

开发者只需要实现:主内容区的内容和菜单的点击变化即可。

API:https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

1. DrawerLayout本身就是一个顶级容器,只需要按照规定的布局方式写布局就会有侧滑效果:

规定:

  • DrawerLayout要包括两个子视图,分别为主内容区布局和侧滑菜单布局,并且主内容区布局要放到侧滑菜单布局的前面;
  • 主内容布局需要设置宽高为match_parent,且不能设置layout_gravity;
  • 侧滑菜单布局一般高度为match_parent,宽度为固定值;
  • 侧滑菜单布局需要设置layout_gravity属性,start表示左边,end表示右边,不设置时会默认显示且不能滑动;
  • 侧滑菜单布局(drawer)只能有一个,多于一个会报错;
  • 主内容布局和侧滑菜单布局,使用何种样式的布局没有强制限制。

<?xml version="1.0" encoding="utf-8"?>
<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/palyer_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".PlayerActivity"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="200dp"
            android:layout_marginLeft="200dp"
            android:text="这是主区域"/>

    </RelativeLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="200dp"
        android:layout_gravity="start"
        android:layout_height="match_parent"
        android:background="#ff00B8D4">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="这是菜单栏"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="菜单一"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="菜单二"/>

    </LinearLayout>

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

2. DrawerLayout.DrawerListener可以监听drawer的状态变化和运动过程

只想继承某个或者某些方法,使用DrawerLayout.SimpleDrawerListener,这是DrawerListener接口的简单实现;

注意:要避免执行昂贵的动画,必须要做动画可以在STATE_IDLE状态时执行;

package com.media.customplayer;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class PlayerActivity extends AppCompatActivity {

    private final String TAG = "PlayerActivity";
    private DrawerLayout mPlayerDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        mPlayerDrawer = (DrawerLayout) findViewById(R.id.palyer_drawer_layout);
        mPlayerDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.v(TAG, "onDrawerSlide: slideOffset = " + slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.v(TAG, "onDrawerOpened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.v(TAG, "onDrawerClosed");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.v(TAG, "onDrawerStateChanged: newState = " + newState);
            }
        });
    }
}

一次展示和隐藏的日志如下:

07-10 09:57:20.336 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerStateChanged: newState = 1
07-10 09:57:20.356 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.045
07-10 09:57:20.366 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.10666667
07-10 09:57:20.386 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.16666667
07-10 09:57:20.406 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.23
07-10 09:57:20.416 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.29666665
07-10 09:57:20.436 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.36166668
07-10 09:57:20.446 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.42333335
07-10 09:57:20.476 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.50166667
07-10 09:57:20.486 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.5516667
07-10 09:57:20.506 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.60333335
07-10 09:57:20.526 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.655
07-10 09:57:20.536 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.7133333
07-10 09:57:20.556 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.7816667
07-10 09:57:20.576 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.8516667
07-10 09:57:20.586 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.92833334
07-10 09:57:20.606 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 1.0
07-10 09:57:20.636 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerOpened
07-10 09:57:20.646 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerStateChanged: newState = 0
07-10 09:57:40.546 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerStateChanged: newState = 1
07-10 09:57:40.566 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.9633333
07-10 09:57:40.586 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.92333335
07-10 09:57:40.596 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.89166665
07-10 09:57:40.616 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.8616667
07-10 09:57:40.636 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.83666664
07-10 09:57:40.646 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.81166667
07-10 09:57:40.666 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.7916667
07-10 09:57:40.686 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.7683333
07-10 09:57:40.696 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.74833333
07-10 09:57:40.716 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.73
07-10 09:57:40.736 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.71666664
07-10 09:57:40.746 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.70166665
07-10 09:57:40.766 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.6933333
07-10 09:57:40.786 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.67833334
07-10 09:57:40.796 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.66333336
07-10 09:57:40.816 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.645
07-10 09:57:40.836 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.62833333
07-10 09:57:40.846 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.615
07-10 09:57:40.866 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.6
07-10 09:57:40.876 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.58666664
07-10 09:57:40.896 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.57166666
07-10 09:57:40.916 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.55333334
07-10 09:57:40.926 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.53333336
07-10 09:57:40.946 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.515
07-10 09:57:40.966 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.49833333
07-10 09:57:40.976 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.48833334
07-10 09:57:40.996 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.47333333
07-10 09:57:41.016 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.46
07-10 09:57:41.026 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.44333333
07-10 09:57:41.046 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.42833334
07-10 09:57:41.066 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.41333333
07-10 09:57:41.076 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.39666668
07-10 09:57:41.096 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.38333333
07-10 09:57:41.116 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.37
07-10 09:57:41.126 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.35666665
07-10 09:57:41.146 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.34833333
07-10 09:57:41.156 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.33333334
07-10 09:57:41.176 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.32
07-10 09:57:41.196 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.30666667
07-10 09:57:41.206 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.29666665
07-10 09:57:41.226 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.28666666
07-10 09:57:41.246 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.27333334
07-10 09:57:41.256 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.25833333
07-10 09:57:41.276 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.23833333
07-10 09:57:41.296 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.22
07-10 09:57:41.306 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.20333333
07-10 09:57:41.326 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.18666667
07-10 09:57:41.346 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.17166667
07-10 09:57:41.356 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.15166667
07-10 09:57:41.376 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.13
07-10 09:57:41.386 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.108333334
07-10 09:57:41.406 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.08833333
07-10 09:57:41.436 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerStateChanged: newState = 2
07-10 09:57:41.446 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.086666666
07-10 09:57:41.456 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.065
07-10 09:57:41.476 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.046666667
07-10 09:57:41.486 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.033333335
07-10 09:57:41.506 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.023333333
07-10 09:57:41.526 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.015
07-10 09:57:41.536 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.01
07-10 09:57:41.556 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.006666667
07-10 09:57:41.576 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.0033333334
07-10 09:57:41.586 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.0016666667
07-10 09:57:41.626 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerSlide: slideOffset = 0.0
07-10 09:57:41.646 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerClosed
07-10 09:57:41.646 27804-27804/com.media.customplayer V/PlayerActivity: onDrawerStateChanged: newState = 0

3. 主动展开:DrawerLayout.openDrawer

主动隐藏:DrawerLayout.closeDrawer

package com.media.customplayer;

import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

public class PlayerActivity extends AppCompatActivity {

    private final String TAG = "PlayerActivity";
    private DrawerLayout mPlayerDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        mPlayerDrawer = (DrawerLayout) findViewById(R.id.palyer_drawer_layout);
        mPlayerDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.v(TAG, "onDrawerSlide: slideOffset = " + slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.v(TAG, "onDrawerOpened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.v(TAG, "onDrawerClosed");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.v(TAG, "onDrawerStateChanged: newState = " + newState);
            }
        });

        mPlayerDrawer.openDrawer(GravityCompat.START, true);

        final Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                mPlayerDrawer.closeDrawer(GravityCompat.START, true);
                timer.cancel();
            }
        };
        timer.schedule(task, 2000);
    }
}

时间: 2024-10-13 23:30:55

Android: DrawerLayout 侧滑菜单栏的相关文章

Material Design之NavigationView和DrawerLayout实现侧滑菜单栏

本文将介绍使用Google最新推出规范式设计中的NavigationView和DrawerLayout结合实现侧滑菜单栏效果,NavigationView是android-support-design包下的一个控件,该包下还有AppBarLayout.CoordinatorLayout.FloatingActionButton.SnackBar.TabLayout控件,也是Google在Android 5.x推荐规范式使用的控件.本系列将逐一介绍每个控件的使用... 好了,先来看看本文最终的效果

google 原生态 抽屉式侧滑菜单 Android DrawerLayout 布局的使用介绍

废话不多说,直接上效果图: 其实谷歌官方已经给出了一个 关于DrawerLayout 使用的例子,只是处于 国内不能访问谷歌官网,看不到详细文档说明,所以在此 简单记录下  侧滑 抽屉式菜单的使用说明 1.由于 DrawerLayout 需要 android.support.v4 包的支持,所以,你的libs 下面不要包含 这个包. 2.首先布局文件如下 <android.support.v4.widget.DrawerLayout xmlns:android="http://schema

ViewDragHelper实践之仿Android官方侧滑菜单NavigationDrawer效果

相信经常使用移动应用的用户都很熟悉侧滑菜单栏, 下拉, 下弹, 上弹等应用场景, 几乎主流的移动应用无论IOS 还是Android都能看到. 2.3以前的时候, 很多第三方比如SlidingMenu, MenuDrawer, ActionbarSherlock等等都很大程度的丰富和深化了这种交互理念.能让小小的屏幕, 容纳更多的交互接口. 也是这种趋势, Android官方在v4终于推出了DrawerLayout. 表示对侧滑的重视与肯定. 唠叨到这了. 去看了DrawerLayout的源码和官

Android UI-SlidingMenu侧滑菜单效果

Android UI-SlidingMenu侧滑菜单效果 本篇博客给大家分享一个效果比较好的侧滑菜单的Demo,实现点击左边菜单切换Fragment. 效果如下: 主Activity代码: package com.infzm.slidingmenu.demo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.View; import android.view.View.On

Android 左右侧滑示例

Android 左右侧滑示例.可以自已设置侧滑的效果,我在这里实现是左右都可以.侧滑出来的界面可以启动activity,或者加载view.侧滑效果是用了碎片规则来处理的.详细注释在代码里面,先看实现效果图,再看项目结构以及资源文件分布.代码只贴部分,提供本文源码下载链接. 源码下载:点击 1.实现效果图 2.项目结构图 3.资源分布图 4.LeftFragment package com.org.fragment; import com.org.activity.R; import com.or

Android DrawerLayout 抽屉

Android DrawerLayout 抽屉 DrawerLayout 在supportV4 Lib中,类似开源slidemenu一样,DrawerLayout父类为ViewGroup,自定义组件基本都是扩展这个类. android.support.v4.widget.DrawerLayout 下面是个简单的用法演示.点左上角的按钮 打开抽屉菜单. 点击对应的ITEM 切换对应的内容,内容显示使用Fragment,这里没用到ActionBar来做切换 <?xml version="1.0

Android DrawerLayout抽屉效果

官网guide:http://developer.android.com/training/implementing-navigation/nav-drawer.html 官网示例:NavigationDrawer.zip android.support.v4.widget.DrawerLayout 抽屉layout.该widget只能实现从左向右.从右向左 openDrawer(), closeDrawer(), isDrawerOpen() 下面贴一下示例的主要的布局文件 和 activit

android官方侧滑菜单DrawerLayout详解

drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物.drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现). drawerLayout的使用很方便,使用drawerLayout的要点如下: 1.drawerLayout其实是

【转】android官方侧滑菜单DrawerLayout详解

原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物.drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着