教你用DrawLayout 实现Android 侧滑菜单
这个侧滑菜单的实现网上也有很多方法,比如最常用的是开源的SlidingMenu . 还有一种实现方式是在一个Activity的布局中分两部分,一个是菜单(menu)的布局,一个是内容(content)的布局。两个布局横向排列,菜单布局在左,内容布局在右。初始化的时候将菜单布局向左偏移,以至于能够完全隐藏,这样内容布局就会完全显示在Activity中。然后通过监听手指滑动事件,来改变菜单布局的左偏移距离,从而控制菜单布局的显示和隐藏。
但是这两种方法都相对比较繁琐,今天给大家介绍一种更为简单的方法。就是直接采用DrawLayout。有关DrawLayout更详细的介绍可以参考API文档:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html 。
首先New一个Android工程,依次实现几个布局。先来看两个子布局。
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="@android:color/transparent"/>
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="每日一文"
android:textAppearance="?android:attr/textAppearanceLarge" >
主界面布局,注释掉的是右边的侧滑,现在实现的是左边的侧滑。
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:id="@+id/fragment_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:id="@+id/menu_layout_left"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#FFFFFF" >
android:id="@+id/menu_listView_l"
android:layout_width="match_parent"
android:layout_height="match_parent" >
然后创建两个类继承Fragment,把两个子布局塞进去。
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.first, null);
}
}
再来看看主类:
public class MainActivity extends FragmentActivity {
public static final String[] TITLES = {"first", "second"};
private DrawerLayout mDrawerLayout;
private RelativeLayout mLeftLayout;
private ListView mLeftListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
mLeftListView.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1, TITLES));
// 监听菜单
mLeftListView.setOnItemClickListener(new DrawerItemClickListenerLeft());
}
private void findViewById() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mLeftLayout = (RelativeLayout) findViewById(R.id.menu_layout_left);
mLeftListView = (ListView) findViewById(R.id.menu_listView_l);
}
public class DrawerItemClickListenerLeft implements OnItemClickListener {
@Override
public void onItemClick(AdapterView
教你用DrawLayout 实现Android 侧滑菜单的更多相关文章
- Android侧滑菜单代码实现
前两天学习了hyman老师讲的Android侧滑菜单的实现,经过自己的整理分享出来给大家学习一下 现在很多APP都有菜单侧滑的功能,本篇文章主要讲解使用自定义的HorizontalScrollView ...
- Android 侧滑菜单的简单实现(SlidingMenu)二
在上一篇博文中已经简单的实现了侧滑菜单,代码也很简单,就几行代码. 这篇文章依然讲侧滑菜单,与前一篇文章不同的是,这篇文章用不同的代码方式来实现侧滑菜单. 在前面的文章中已经用了在Activity中通 ...
- Android 侧滑菜单的简单实现(SlidingMenu)
在我还没有学习Android的时候就用过侧滑菜单的APP,当时第一个感觉是:哇塞,这效果不错!当然,现在自己都已经学Android了,这效果当然也要做出来啊~ SlidingMenu是一种比较新的设置 ...
- android侧滑菜单笔记
一.SlidingPaneLayout v4包下的控件,使用简单,功能简洁.官方文档明确说明该控件只能左侧滑动.使用如下: <android.support.v4.widget.SlidingP ...
- android 侧滑菜单
就是用手一滑才出现,占手机半个多屏幕的菜单.为了美观和页面转跳,很多时候要用到. 实现的话就是使用官方的DrawerLayout,注意这个布局一定要是最顶层的布局. 在DrawerLayout里面直接 ...
- Android侧滑菜单和轮播图之滑动冲突
接手一个项目,有一个问题需要修改:轮播图不能手动滑动,手动滑动轮播图只会触发侧滑菜单. 猜测:viewpager控件(轮播图)的触摸事件被SlidingMenu控件(侧滑菜单,非第三方项目,乃是上个开 ...
- Android滑动菜单框架完全解析,教你如何一分钟实现滑动菜单特效
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744400 之前我向大家介绍了史上最简单的滑动菜单的实现方式,相信大家都还记得.如 ...
- Android组件——使用DrawerLayout仿网易新闻v4.4侧滑菜单
摘要: 转载请注明出处:http://blog.csdn.net/allen315410/article/details/42914501 概述 今天这篇博客将记录一些关于DrawerL ...
- Android之自定义侧滑菜单
先来上图: 我们把主界面从左向右拉动,可以看到地下有一层菜单页,从透明渐渐变得不透明,从小渐渐变大,感觉上觉得菜单页是从屏幕外面被拉到屏幕中的.下面的代码实现这个DEMO: 首先是自定义控件Slidi ...
随机推荐
- Hbase—— rowkey 过滤器(rowfilter)
1.RowFilter 提取rowkey以01结尾数据Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStri ...
- 团体程序设计天梯赛 L3-004. 肿瘤诊断
数组的大小不能开太大,否则会出现段错误 用bfs而不用dfs,dfs存储太多中间过程,会超内存 #include <stdio.h> #include <stdlib.h> # ...
- ural 2032 Conspiracy Theory and Rebranding (数学水题)
ural 2032 Conspiracy Theory and Rebranding 链接:http://acm.timus.ru/problem.aspx?space=1&num=2032 ...
- QCon技术干货:个推基于Docker和Kubernetes的微服务实践
2016年伊始,Docker无比兴盛,如今Kubernetes万人瞩目.在这个无比需要创新与速度的时代,由容器.微服务.DevOps构成的云原生席卷整个IT界.在近期举办的QCon全球软件开发大会上, ...
- Java入门:读写文本文件
文本文件的读写是学习java必须掌握的一项基本技术,因为在项目中时常会涉及到文本文件的读写. 一.使用FileWriter写文件 1.FileWriter类 [功能] FileWriter类专门用来写 ...
- css--display属性中inline-block与inline的区别
inline-block 与 inline 的区别: inline-block 与inline 效果类似,但是inline-block是可以设定宽度和高度的!!而行内元素(也就是inline)是无法设 ...
- Python 装饰器(进阶篇)
装饰器是什么呢? 我们先来打一个比方,我写了一个python的插件,提供给用户使用,但是在使用的过程中我添加了一些功能,可是又不希望用户改变调用的方式,那么该怎么办呢? 这个时候就用到了装饰器.装饰器 ...
- mysql 对应 binlog 查看
什么是 binlog binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格 ...
- java8 新特性 Stream
1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel agg ...
- MESS-配置
Author:KillerLegend From:http://www.cnblogs.com/killerlegend/p/3824989.html Date:2014.7.4 Part A 第一部 ...