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);
}
}
Android: DrawerLayout 侧滑菜单栏的更多相关文章
- Android DrawerLayout侧滑菜单
本教程已经录制视频,欢迎大家观看我在CSDN学院录制的课程:http://edu.csdn.net/lecturer/944
- Material Design之NavigationView和DrawerLayout实现侧滑菜单栏
本文将介绍使用Google最新推出规范式设计中的NavigationView和DrawerLayout结合实现侧滑菜单栏效果,NavigationView是android-support-design ...
- android DrawerLayout 侧边栏实现
现在实现侧边栏比较简单了,官方提供的DrawerLayout可以很方便实现. 主要实现方法是:用DrawerLayout 作为界面根控件.在DrawerLayout里面第一个View为当前界面主内容: ...
- Android SlidingMenu侧滑菜单使用
把下载的侧滑菜单压缩包打开,会有一个library文件夹,在eclipse中import existing android code into workspace,导入library文件夹,并且选择作 ...
- Android DrawerLayout 抽屉
Android DrawerLayout 抽屉 DrawerLayout 在supportV4 Lib在.类似的开源slidemenu如,DrawerLayout父类ViewGroup,自定义组件基本 ...
- Android - DrawerLayout
Android DrawerLayout 的使用 Android L Android Studio 1.4 从主视图左侧能抽出一个导航栏,效果图: 点击后弹出新界面: 新界面也可以抽出左侧导航栏 ...
- Android DrawerLayout 高仿QQ5.2双向侧滑菜单
1.概述 之前写了一个Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 ,恰逢QQ5.2又加了一个右侧菜单,刚好看了下DrawerLayout,一方面官方的东西,我都比较感兴趣:另一方面 ...
- 【转】android官方侧滑菜单DrawerLayout详解
原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html drawerLayout是Support ...
- android官方侧滑菜单DrawerLayout详解
drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物.d ...
随机推荐
- 怎样在iis中发布asp.net网站
以windows server2003.vs2008和sql servber2005为例.将开发完成的asp.net网站发布,将发布包放在windows server2003服务器的文件夹下.将web ...
- vue原来可以这样上手
今儿与一群友讨论vue相关问题让我思量极深,1.我们是否在争对性解决问题或者说是帮助别人:2.我们是否在炫耀自己的技能.以下是被戏剧化的对白: "群友":最近按照vue官网示例学习 ...
- Eclipse安装git
用Eclipse开发,如果需要团队协作,git作为分布式版本管理工具就是个比较好的选择.下面简单介绍一下git插件的安装方法: 1.Help -- install new software 打开插件安 ...
- 玩转微信小程序
原文链接 2007 年 1 月 9 号,苹果一代在功能机盛行的年代中出世. 2017 年 1 月 9 号,微信小程序在重型app风靡的压力下上线. 苹果的出世掀起了互联网一波又一波的浪潮,而微信小程序 ...
- 基於tiny4412的Linux內核移植 --- 实例学习中断背后的知识(1)
作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台 tiny4412 ADK Linux-4.9 概述 前面几篇博文列举了在有设备树的时候,gpio中断的 ...
- KB奇遇记(9):艰难的上线
经历了非常多的磨难,系统也“如约“在2017年01月01日勉强上线了.尽管我认为它还不到上线的程度,条件不具备,但上头的指令下来和计划便是在这一天.整个上线过程从2016年3月8号开始到上线日,扣除中 ...
- WebForm 三级联动
三级联动 数据库根据父级代号条件写查询 返回list<>集合 方法一: 创建三个下拉列表: ※AutoPostBack:否发生自动回传到服务器的操作.如果把该属性设置为 TRUE,则启用自 ...
- JDK分析工具&JVM垃圾回收(转)
转自:http://blog.163.com/itjin45@126/blog/static/10510751320144201519454/ 官方手册:http://docs.oracle.com/ ...
- npm 不是内部命令
最近办公室流行给电脑装win10系统,于是在重新装好电脑系统后,再次运行thinkjs项目的时候,就发现了之前做过的项目打不开了,待再确认问题出在哪里的时候,才发现”nodejs以及npm不是内部或者 ...
- 学习Cocos2d的博客 --推荐
http://xiaominghimi.blog.51cto.com/2614927/d-6/p-1