侧滑的方案有很多种,早期的开源SliddingMenu,以及后来的DrawerLayout以及NavigationView等都可实现侧滑效果,这里介绍的是DrawerLayout,下一节将介绍NavigationView

原理

DrawerLayout位于v4包,为了做到最低限度的兼容,使得更低版本的Android也可以使用这个侧滑效果

其就是一个自定义的容器,继承自ViewGroup

在解析DrawerLayout布局的时候,根据android:layout_gravity="start"标签确定主布局和侧滑布局

例如下面的布局,就直接呈现出一个侧滑菜单

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <!--内容部分-->
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:orientation="vertical">
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:background="@android:color/holo_green_light"
  14. android:text="内容部分" />
  15. </LinearLayout>
  16. <!--侧滑菜单部分-->
  17. <LinearLayout
  18. android:layout_width="200dp"
  19. android:layout_height="match_parent"
  20. android:layout_gravity="start"
  21. android:orientation="vertical">
  22. <TextView
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:background="@android:color/holo_blue_light"
  26. android:text="item 1" />
  27. <TextView
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:background="@android:color/holo_orange_light"
  31. android:text="item 2" />
  32. <TextView
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:background="@android:color/holo_red_light"
  36. android:text="item 3" />
  37. </LinearLayout>
  38. </android.support.v4.widget.DrawerLayout>

直接运行,效果图如下

使用Toolbar

因为要使用Toolbar,就要去掉其ActionBar,故直接修改style文件

  1. <resources>
  2. <!-- Base application theme. -->
  3. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  4. <!-- Customize your theme here. -->
  5. <item name="colorPrimary">@color/colorPrimary</item>
  6. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  7. <item name="colorAccent">@color/colorAccent</item>
  8. </style>
  9. </resources>

更改布局,使其符合MD标准

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <android.support.v7.widget.Toolbar
  7. android:id="@+id/tool_bar"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:background="?attr/colorPrimary" />
  11. <android.support.v4.widget.DrawerLayout
  12. android:id="@+id/drawer_layout"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent">
  15. <!--内容部分-->
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:orientation="vertical">
  20. <TextView
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:background="@android:color/holo_green_light"
  24. android:text="内容部分" />
  25. </LinearLayout>
  26. <!--侧滑菜单部分-->
  27. <LinearLayout
  28. android:layout_width="200dp"
  29. android:layout_height="match_parent"
  30. android:layout_gravity="start"
  31. android:background="@color/colorPrimary"
  32. android:orientation="vertical"
  33. android:paddingTop="50dp">
  34. <TextView
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:background="@android:color/holo_blue_light"
  38. android:text="item 1" />
  39. <TextView
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:background="@android:color/holo_orange_light"
  43. android:text="item 2" />
  44. <TextView
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:background="@android:color/holo_red_light"
  48. android:text="item 3" />
  49. </LinearLayout>
  50. </android.support.v4.widget.DrawerLayout>
  51. </LinearLayout>

在活动处设置Toolbar

  1. public class MainActivity extends AppCompatActivity {
  2. private Toolbar toolbar;
  3. private DrawerLayout drawerLayout;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. toolbar = findViewById(R.id.tool_bar);
  9. //将ActionBar替换成Toolbar
  10. setSupportActionBar(toolbar);
  11. drawerLayout = findViewById(R.id.drawer_layout);
  12. ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,
  13. drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
  14. //同步状态
  15. drawerToggle.syncState();
  16. //给侧滑控件设置监听
  17. drawerLayout.setDrawerListener(drawerToggle);
  18. }
  19. }

至此便完成了侧滑功能的实现,其效果图如下

实现右侧滑入,这个其实很简单,将之前设置的android:layout_gravity="start"更改为android:layout_gravity="end"便实现右侧滑入,同时还可以实现左右都可以滑入

实现类似QQ的效果

在监听的状态里面设置即可

  1. public class MainActivity extends AppCompatActivity {
  2. private Toolbar toolbar;
  3. private DrawerLayout drawerLayout;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. toolbar = findViewById(R.id.tool_bar);
  9. //将ActionBar替换成Toolbar
  10. setSupportActionBar(toolbar);
  11. drawerLayout = findViewById(R.id.drawer_layout);
  12. ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,
  13. drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
  14. //同步状态
  15. drawerToggle.syncState();
  16. //给侧滑控件设置监听
  17. //drawerLayout.setDrawerListener(drawerToggle);
  18. drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
  19. @Override
  20. public void onDrawerSlide(@NonNull View view, float slideOffset) {
  21. //滑动过程中回调,其中slideOffset的值为 0~1
  22. View content = drawerLayout.getChildAt(0);
  23. View menu = view;
  24. float scale = 1 - slideOffset;//1~0
  25. float leftScale = (float) (1f - 0.3 * scale);//1~0.7
  26. //float rightScale = (float) (0.7f + 0.3 * scale);//0.7~1
  27. menu.setScaleX(leftScale);//1~0.7
  28. menu.setScaleY(leftScale);//1~0.7
  29. //content.setScaleX(rightScale);
  30. //content.setScaleY(rightScale);
  31. content.setTranslationX(menu.getMeasuredWidth() * (1 - scale));//0~width
  32. }
  33. @Override
  34. public void onDrawerOpened(@NonNull View view) {
  35. //打开时回调
  36. }
  37. @Override
  38. public void onDrawerClosed(@NonNull View view) {
  39. //关闭时回调
  40. }
  41. @Override
  42. public void onDrawerStateChanged(int i) {
  43. //状态改变时回调
  44. }
  45. });
  46. }
  47. }

效果图如下

高级UI-DrawerLayout侧滑的更多相关文章

  1. firefox 扩展开发笔记(三):高级ui交互编程

    firefox 扩展开发笔记(三):高级ui交互编程 前言 前两篇链接 1:firefox 扩展开发笔记(一):jpm 使用实践以及调试 2:firefox 扩展开发笔记(二):进阶开发之移动设备模拟 ...

  2. Android 高级UI设计笔记07:RecyclerView 的详解

    1. 使用RecyclerView       在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...

  3. 高级UI晋升之自定义View实战(六)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义V ...

  4. iOS开发——高级UI&带你玩转UITableView

    带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...

  5. Android: DrawerLayout 侧滑菜单栏

    DrawerLayout是SupportLibrary包中实现的侧滑菜单效果的控件. 分为主内容区域和侧边菜单区域 drawerLayout本身就支持:侧边菜单根据手势展开与隐藏, 开发者只需要实现: ...

  6. Android学习总结——DrawerLayout 侧滑栏点击事件穿透

    使用DrawerLayout实现侧滑栏功能时,点击侧滑栏空白处时,主界面会获得事件. 解决方法:侧滑栏布局添加 android:clickable="true"

  7. DrawerLayout侧滑

    DrawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说DrawerLayout是因为第三方控件如SlidingMenu等出现之后,google借鉴而出现的产物.D ...

  8. 高级UI晋升之布局ViewGroup(四)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从LinearLayout.RelativeLayout.FrameLa ...

  9. 高级UI晋升之常用View(三)中篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...

  10. 高级UI晋升之View渲染机制(二)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 优化性能一般从渲染,运算与内存,电量三个方面进行,今天开始说聊一聊Android ...

随机推荐

  1. SYSTEM_INFORMATION_CLASS

    source: https://github.com/processhacker/processhacker/blob/master/phnt/include/ntexapi.h // rev // ...

  2. leetcode解题报告(15):Third Maximum Number

    描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...

  3. Educational Codeforces Round 55 题解

    题解 CF1082A [Vasya and Book] 史上最难A题,没有之一 从题意可以看出,翻到目标页只有三种办法 先从\(x\)到\(1\),再从\(1\)到\(y\) 先从\(x\)到\(n\ ...

  4. ubuntu中防火墙iptables配置

    特别说明:此文章完全转载于https://www.cnblogs.com/EasonJim/p/6851007.html 1.查看系统是否安装防火墙 root@localhost:/usr# whic ...

  5. js 删除节点

    亲身经历,寻得此法,告知大家=============== 在javascript操作dom树的时候可能会经常遇到增加,删除节点的事情,比如一个输入框后一个增加按钮,一个删除按钮,点击增加就增加 个输 ...

  6. centos7 安装 mysql5.6(MySQL-5.6.44-1.el7.x86_64.rpm-bundle.tar)

    1.卸载MariaDB rpm -qa | grep -i mariadb rpm -e --nodeps mariadb-libs--.el7.x86_64 2.卸载已有Mysql 卸载旧版本mys ...

  7. javaSE集合---进度2

    一.集合框架 1.特点 对象封装数据,对象多了也需要存储,集合用于存储对象. 对象的个数确定可以使用数组,但是不确定的话,可以用集合,因为集合是可变长度的. 2.集合和数组的区别 数组是固定长度的,集 ...

  8. RK3288 查看ddr信息

    转载请注明出处:https://www.cnblogs.com/lialong1st/p/10910949.html CPU:RK3288 系统:Android 5.1 1.查看ddr驱动版本号.容量 ...

  9. python matplotlib(数据可视化)

    吐槽 网上搜了不少matplotlib安装方法(不信,你可以试试.) 我只能说,除了太繁琐,就是没什么用! 如果你是python3.6.5版本 我给你最最最正确的建议: 直接打开cmd,找到pip用命 ...

  10. uefi是如何启动linux内核的?

    答:uefi启动linux内核有两条路径: 1. uefi直接进入uefi shell来启动linux内核 2. uefi直接进入uefi shell启动grub启动器,然后进入grub shell启 ...