DrawerLayout预览

DrawerLayout主要功能就是 实现侧滑菜单效果的功能,并且可以通过增加一些设置来实现高大上的效果,那么就请看动态图:

 

注意左上角那个图标,有木有很好玩,哈哈...

接下来就介绍如何实现这一功能

1. 在项目对应的build.gradle中添加依赖

    dependencies {
...//其他代码
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
...//其他代码
}

2. 添加ToolBar,创建toolbar.xml文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="资讯"
app:titleTextColor="#fff">
</android.support.v7.widget.Toolbar>
</RelativeLayout>

3. 在main.xml中添加DrawerLayout

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- 添加ToolBar -->
<include layout="@layout/toolbar"/> <!--添加DrawerLayout-->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- 一般第一个位置的代表 主内容 -->
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout> <!-- 左侧菜单(设置layout_gravity 为left) -->
<RelativeLayout
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left">
</RelativeLayout> <!-- 右侧菜单(设置layout_gravity 为right) -->
<RelativeLayout
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right">
</RelativeLayout> </android.support.v4.widget.DrawerLayout>
</LinearLayout>

DrawerLayout一般分为三个部分 主内容,左侧菜单,右侧菜单
每个部分的内容自行设置,我是采用Fragment方式设置内容,这里仅供参考

    //新建Fragment,具体内容我就不详细说了
fragmentMain = new FragmentMain();
//添加内容,比较简单的
getSupportFragmentManager().beginTransaction().replace(R.id.main, fragmentMain).commit();

到此为止已经初步实现了侧滑菜单的功能,来看一下效果

DrawerLayout初效果.gif

然后,就是给侧滑按钮添加效果了

1. 在此之前要进行view的初始化

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
toolbar = (Toolbar) this.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

2. 通过ActionBarDrawerToggle来完成效果,操作很简单

    mToggle = new ActionBarDrawerToggle(HomeActivity.this,
mDrawerLayout,
toolbar,
R.string.open,
R.string.close);
mToggle.syncState();
mDrawerLayout.addDrawerListener(mToggle);

这样就结束了

最后就是解决DrawerLayout不能全屏滑动的问题

private void setDrawerLeftEdgeSize (Activity activity, DrawerLayout drawerLayout, float displayWidthPercentage) {
if (activity == null || drawerLayout == null) return;
try {
// 找到 ViewDragHelper 并设置 Accessible 为true
Field leftDraggerField =
drawerLayout.getClass().getDeclaredField("mLeftDragger");//Right
leftDraggerField.setAccessible(true);
ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(drawerLayout); // 找到 edgeSizeField 并设置 Accessible 为true
Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize");
edgeSizeField.setAccessible(true);
int edgeSize = edgeSizeField.getInt(leftDragger); // 设置新的边缘大小
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
edgeSizeField.setInt(leftDragger, Math.max(edgeSize, (int) (displaySize.x *
displayWidthPercentage)));
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}

直接调用这个方法即可!最后一个参数 传 1,即可实现全屏滑动。如果你想让右侧菜单也是全屏,只要将对应的 "mLeftDragger" 改为 "mRightDragger"。

最新文章

  1. [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务
  2. fcc的中级算法题
  3. hdu 5861 Road 两棵线段树
  4. Ubuntu12.04 使用中遇到的问题
  5. 用bat文件将本地sql在远程oracle上执行
  6. php5下载,apache2.4与php5整合
  7. 关于switch的思考和总结
  8. hdu3182 状态压缩水题
  9. Array对象 识记
  10. JavaScript关于js垃圾回收
  11. ora-01190和ora-01110的解决方法
  12. WPF DEV dxc:ChartControl 柱状图
  13. JVM 字节码(一)字节码规范
  14. outlook vba开发要点
  15. android TextView 例子代码(文字图片、文字省略、文字滚动)
  16. C#操作json类型数据
  17. Eclipse 快速提取一个方法 (重构)
  18. 利用trace重建控制文件
  19. JavaScript设计模式-8.链式调用
  20. 从零开始制作H5人脸融合小游戏

热门文章

  1. (转载) IaaS, PaaS, Saas
  2. socket 的通信过程
  3. Asp.net Core 源码-SessionExtensions
  4. MySQL安装部署
  5. CentOS7安装GitLab、汉化、邮箱配置及使用(转载)
  6. day27-1 numpy模块
  7. jquery ajax 全介绍
  8. ELO kernels 记录
  9. nmcli connection modify eth1 ipv4.addr &quot;192.168.31.23&quot; ipv4.method manual
  10. Linux 内核管理