教你用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 ...
随机推荐
- HDFS问题集(一),使用命令报错:com.google.protobuf.ServiceException:java.lang.OutOfMemoryError:java heap space
仅个人实践所得,若有不正确的地方,欢迎交流! 一.起因 执行以下两条基本的HDFS命令时报错 hdfs dfs -get /home/mr/data/* ./ hdfs dfs -ls /home/m ...
- BZOJ2525 [Poi2011]Dynamite 【二分 + 贪心】
题目链接 BZOJ2525 题解 就是要求所有有炸弹的点到点燃点距离最大值最小 显然二分答案距离\(D\) 然后按深度排序,贪心点燃当前没覆盖的深度最深的点往上第\(D\)层的点 每覆盖一个点要标记其 ...
- mimikazhi Kerberos Modules
Kerberos Modules 1. .#####. mimikatz 2.0 alpha (x64) release "Kiwi en C" (Oct 9201500 ...
- 调用Android系统设置项
Intent mIntent = new Intent(); ComponentName comp = new ComponentName("com.android.settings&quo ...
- 【arc102E】Stop. Otherwise...
Portal --> arc102E Description 有\(N\)个位置,每个位置可以填一个\(1\sim K\)的数,要求对于每一个\(i\in [2,2K]\),求出任意两个位置的和 ...
- 【数学】【CF1096C】 Polygon for the Angle
Description 给定一个角度 \(\theta\),请你寻找一个正 \(n\) 边型,满足在这个正 \(n\) 边型上找三个顶点 \(A,B,C\) (可以不相邻),使得 \(\angle A ...
- golang简单实现二叉树的数据添加和遍历
代码实现 package tree import "fmt" type Node struct { elem interface{} left, right *Node } typ ...
- linux命令总结之date命令
命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the system date and time 指令所在路径:/bin/date 命令语法: date [OP ...
- C# 在程序中控制IIS服务或应用程序池关闭重启
//停止IIS服务 ServiceController sc = new ServiceController("iisadmin"); if(sc.Status=ServiceCo ...
- uboot常用命令详解
dnw:在进入系统之前进入指令行,输入该指令可下载烧录文件. re:重新启动嵌入式系统. printenv:打印当前系统环境变量. setenv:设置环境变量,格式:setenv name value ...