高级UI-DrawerLayout侧滑
侧滑的方案有很多种,早期的开源SliddingMenu,以及后来的DrawerLayout以及NavigationView等都可实现侧滑效果,这里介绍的是DrawerLayout,下一节将介绍NavigationView
原理
DrawerLayout位于v4包,为了做到最低限度的兼容,使得更低版本的Android也可以使用这个侧滑效果
其就是一个自定义的容器,继承自ViewGroup
在解析DrawerLayout布局的时候,根据android:layout_gravity="start"标签确定主布局和侧滑布局
例如下面的布局,就直接呈现出一个侧滑菜单
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--内容部分-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:text="内容部分" />
</LinearLayout>
<!--侧滑菜单部分-->
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:text="item 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
android:text="item 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="item 3" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
直接运行,效果图如下

使用Toolbar
因为要使用Toolbar,就要去掉其ActionBar,故直接修改style文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
更改布局,使其符合MD标准
<?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">
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--内容部分-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:text="内容部分" />
</LinearLayout>
<!--侧滑菜单部分-->
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:paddingTop="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:text="item 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
android:text="item 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="item 3" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
在活动处设置Toolbar
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.tool_bar);
//将ActionBar替换成Toolbar
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
//同步状态
drawerToggle.syncState();
//给侧滑控件设置监听
drawerLayout.setDrawerListener(drawerToggle);
}
}
至此便完成了侧滑功能的实现,其效果图如下

实现右侧滑入,这个其实很简单,将之前设置的android:layout_gravity="start"更改为android:layout_gravity="end"便实现右侧滑入,同时还可以实现左右都可以滑入
实现类似QQ的效果
在监听的状态里面设置即可
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.tool_bar);
//将ActionBar替换成Toolbar
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
//同步状态
drawerToggle.syncState();
//给侧滑控件设置监听
//drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float slideOffset) {
//滑动过程中回调,其中slideOffset的值为 0~1
View content = drawerLayout.getChildAt(0);
View menu = view;
float scale = 1 - slideOffset;//1~0
float leftScale = (float) (1f - 0.3 * scale);//1~0.7
//float rightScale = (float) (0.7f + 0.3 * scale);//0.7~1
menu.setScaleX(leftScale);//1~0.7
menu.setScaleY(leftScale);//1~0.7
//content.setScaleX(rightScale);
//content.setScaleY(rightScale);
content.setTranslationX(menu.getMeasuredWidth() * (1 - scale));//0~width
}
@Override
public void onDrawerOpened(@NonNull View view) {
//打开时回调
}
@Override
public void onDrawerClosed(@NonNull View view) {
//关闭时回调
}
@Override
public void onDrawerStateChanged(int i) {
//状态改变时回调
}
});
}
}
效果图如下

高级UI-DrawerLayout侧滑的更多相关文章
- firefox 扩展开发笔记(三):高级ui交互编程
firefox 扩展开发笔记(三):高级ui交互编程 前言 前两篇链接 1:firefox 扩展开发笔记(一):jpm 使用实践以及调试 2:firefox 扩展开发笔记(二):进阶开发之移动设备模拟 ...
- Android 高级UI设计笔记07:RecyclerView 的详解
1. 使用RecyclerView 在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...
- 高级UI晋升之自定义View实战(六)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义V ...
- iOS开发——高级UI&带你玩转UITableView
带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...
- Android: DrawerLayout 侧滑菜单栏
DrawerLayout是SupportLibrary包中实现的侧滑菜单效果的控件. 分为主内容区域和侧边菜单区域 drawerLayout本身就支持:侧边菜单根据手势展开与隐藏, 开发者只需要实现: ...
- Android学习总结——DrawerLayout 侧滑栏点击事件穿透
使用DrawerLayout实现侧滑栏功能时,点击侧滑栏空白处时,主界面会获得事件. 解决方法:侧滑栏布局添加 android:clickable="true"
- DrawerLayout侧滑
DrawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说DrawerLayout是因为第三方控件如SlidingMenu等出现之后,google借鉴而出现的产物.D ...
- 高级UI晋升之布局ViewGroup(四)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从LinearLayout.RelativeLayout.FrameLa ...
- 高级UI晋升之常用View(三)中篇
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...
- 高级UI晋升之View渲染机制(二)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 优化性能一般从渲染,运算与内存,电量三个方面进行,今天开始说聊一聊Android ...
随机推荐
- MongoDB 查看集合是否分片
MongoDB会把分片过的集合保存在config.collection集合中,若需要查看分片键,则需要根据该集合进行查找.官方的其他很多分片快捷命令也都处于config库 三种方式 1.去config ...
- 一个milller_rabin模板
#include <iostream> #include <cstdlib> #include <stdio.h> #include <algorithm&g ...
- About & Ideas & Queries
About Blog主现高一,文化课和OI啥都不会 本Blog主太懒,所以很多内容都缩在一个文章里,如数学.图论大礼包 https://wenku.baidu.com/view/56d76029647 ...
- 爬虫(十八):scrapy分布式部署
scrapy部署神器-scrapyd -->GitHub地址 -->官方文档 一:安装scrapyd 安装:pip3 install scrapyd 这里我在另外一台ubuntu lin ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1)
Virtual participate 的,D题不会做,打了1:30就打不动了,过了ABCE. A - CME 题意:? 题解:? void test_case() { int n; scanf(&q ...
- java四种对象引用类型
java四种对象引用类型 对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序 ...
- NetCore与 NET Framework 不同的地方
.net core 2.0没有了request.inputstream但是可以用request.body替代dataset 没有查看视图了控制台程序默认生成是dll 文件 public string ...
- kafka 讲讲acks参数对消息持久化的影响
目录 (0)写在前面 (1)如何保证宕机时数据不丢失? (2)多副本冗余的高可用机制 (3)多副本之间数据如何同步? (4)ISR到底指的什么东西? (5)acks参数的含义? (6)最后的思考 ...
- springMVC_注解方式搭建基础环境
---恢复内容开始--- 一.jar包环境,web配置文件和Spring-MVC配置文件的,相关的modelAndview 1.配置DispatcherServlet <servlet> ...
- python何时用list,dict,set
从读取的角度来讲: 看是用来随机读取(查询)还是连续读取. list数组集中存放,连续读取效率高(具体还没测试,理论上应该如此). dict散列表,使用hash计算存放的位置,随机读取效率高. 随机读 ...