Android开发实战之拥有Material Design风格的侧滑布局
在实现开发要求中,有需要会使用抽屉式布局,类似于QQ5.0的侧滑菜单,实现的方式有很多种,可以自定义控件,也可以使用第三方开源库。
同样的谷歌也推出了自己的侧滑组件——DrawLayout,使用方式也很简单,配合着toolbar有着不一样的滑动效果,所以推荐大家使用。
如下是效果图,是不是更具交互性,更加酷炫?

**首先是XML**
drawlayout分为两部分:侧滑界面和内容界面,所以drawlayout中应该有两个布局,在侧滑的布局中设置属性:android:layout_gravity="start",就可以实现侧滑了。
下面是XML文件,仅作参考:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dl_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_content">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cl_content">
<include layout="@layout/head_home"/>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/fl_content"></FrameLayout>
<android.support.v4.app.FragmentTabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:id="@+id/ft_host">
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nv_left_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="#333"
app:itemTextColor="#333"
app:headerLayout="@layout/left_home_top"
app:menu="@menu/left_home_botton"
/>
</android.support.v4.widget.DrawerLayout>
**代码部分**
接下来就是让toolbar和drawlayout关联起来。这部分也很简单。让toolbar代替actionbar。这里有个注意点,不能让Activity继承Activity,而是继承于它的子类AppCompatActivity,否则是找不到setSupportActionBar(toolbar)这个方法的,当我们关联起来之后,就可以使用ActionBarDrawerToggle了,它实现了
drawlayout的滑动监听,谷歌也将它和Material Design风格的动画特效关联起来了。
当我们实现了这一步时,让我们看看效果:

可以发现,按钮被挡住了,如果你用过印象笔记的客户端,你会发现,它的侧滑按钮也被挡住了,所以一点都不酷炫了!!!!!
但是作为一个追求完美无bug的安卓开发人员是绝对不允许这样的事的。
**重写onDrawerSlide**
通过重写onDrawerSlide方法重新获取滑动范围,让他贴着内容界面划出。代码也很简单这里贴出主要代码:
private void initView() {
setSupportActionBar(toolbar);
drawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,
R.string.open,R.string.close){
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
WindowManager windowManager= (WindowManager) getSystemService(
getApplicationContext().WINDOW_SERVICE);
Display display=windowManager.getDefaultDisplay();
linearLayout.layout(navigationView.getRight(),
0,
display.getWidth()+navigationView.getRight(),
display.getHeight());
super.onDrawerSlide(drawerView, slideOffset);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
最终的效果图就是开篇的效果,是不是很酷炫,用户体验也比传统的侧滑好的多了。最后多谢浏览,我会继续加油哒!
Android开发实战之拥有Material Design风格的侧滑布局的更多相关文章
- Android开发实战之拥有Material Design风格的折叠布局
关于折叠布局,也许你并不陌生,最新版的陌陌,或者一些其他的社交APP都有一个折叠布局.折叠布局,让我们的APP更加具有交互性,同时也更加美观,先来展示一下效果图: 这是我个人做的一个APP主界面,可以 ...
- 创建Material Design风格的Android应用--应用主题
本人全部文章首先公布于个人博客,欢迎关注,地址:http://blog.isming.me 昨天正式公布了android 5,同一时候android developer站点也更新了,添加了创建Mate ...
- [原创]自定义view之:快速开发一款Material Design风格的dialog的开源项目MDDialog
随着google开始主导Material Design风格的设计,越来越多的app开始使用Material Design风格来设计自己的UI.虽然在Android Studio中集成了多种快速开发框架 ...
- Android实现Material Design风格的设置页面(滑动开关控件)
前言 本文链接 http://blog.csdn.net/never_cxb/article/details/50763271 转载请注明出处 參考了这篇文章 Material Design 风格的设 ...
- 开发Google Material Design风格的WPF程序
今天在网上看到了一个Material Design风格的WPF皮肤,看上去还是挺不错的 这个项目是开源的,感兴趣的朋友可以下载试下: https://github.com/ButchersBoy/Ma ...
- Material Design风格的水波涟漪效果(Ripple Effect)的实现
Material Design是Google在2014年Google I/O大会上推出的一套全新的设计语言,经过接近两年的发展,可谓是以燎原之势影响着整个设计交互生态,和Material Design ...
- Material Design风格登录注册
本文实现了以下功能 完整的代码和样例托管在Github 当接口锁定时,防止后退按钮显示在登录Activity 上. 自定义 ProgressDialog来显示加载的状态. 符合材料设计规范. 悬浮标签 ...
- 自定义 Material Design风格的提示框
关闭 自定义 Material Design风格的提示框 2016-04-24 10:55 152人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 其实在14年谷歌 ...
- 基于React Native的Material Design风格的组件库 MRN
基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...
随机推荐
- web.xml中配置classpath:和classpath*:的区别和意思
首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...
- vuejs2.0的生命周期解读
每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 DOM .下图展示的就是一个v ...
- Qt学习之秒表的实现(StopWatch) (转)
秒表对于我来说并不陌生,在之前自己学习单片机时,实现过秒表和数字钟:基本思路:开启单片机带的定时器,并设置它没10ms溢出一次,分别用三个变量hour,minute,secong记录秒表的时分秒,然后 ...
- 通过Authentication Challenge来信任自签名Https证书
在开发阶段我们我们经常使用自签名的证书来部署我们的后台rest api.但是在iOS中调用的时候就会因为证书不被信任而调用api不成功.这时候我们就需要通过实现某些网络回调函数来自定义证书的验证逻辑. ...
- 树的遍历——pat1043
http://pat.zju.edu.cn/contests/pat-a-practise/1043 给予N个数字组成二叉搜索树,判断这个数列是否由先序遍历得出或是镜像先序遍历得出,若是则输出相应的后 ...
- php 常用方法
//返回json数据给js function json_output($err_code = 0 , $error_message = '' , $data = [] , $redirect = '' ...
- (转)win7+iis7.5+asp.net下 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案
本文转载自:http://www.cnblogs.com/finesite/archive/2011/01/28/1946940.html 网上搜的解决方案但在我的环境下仍旧没有解决,我的方法如下: ...
- Spring AOP两种实现方式
一. AOP 概念: Spring AOP 即Aspect Oriented Programming(面向切面编程), 实现方式分为两种: 1. 注解(Annotation) 2. 配置(Config ...
- struts2学习(10)struts2国际化
一.国际化简介: 二.struts2国际化设置: struts.xml: <?xml version="1.0" encoding="UTF-8" ?&g ...
- [Java]基础.端口
Map<String,String[]> map = request.getParameterMap(); BeanUtils.populate(user,map); // 遍历 use ...