现在的APP越来越注重用户体验,百度视频客户端有一个特效还是挺吸引人的,在主界面手指向右滑动,就可以将菜单展示出来,而主界面会被隐藏大部分,但是仍有左侧的一小部分同菜单一起展示。类似的还有天天动听,人人的客户端。

这个侧滑菜单的实现网上也有很多方法,比如最常用的是开源的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 侧滑菜单的更多相关文章

  1. Android侧滑菜单代码实现

    前两天学习了hyman老师讲的Android侧滑菜单的实现,经过自己的整理分享出来给大家学习一下 现在很多APP都有菜单侧滑的功能,本篇文章主要讲解使用自定义的HorizontalScrollView ...

  2. Android 侧滑菜单的简单实现(SlidingMenu)二

    在上一篇博文中已经简单的实现了侧滑菜单,代码也很简单,就几行代码. 这篇文章依然讲侧滑菜单,与前一篇文章不同的是,这篇文章用不同的代码方式来实现侧滑菜单. 在前面的文章中已经用了在Activity中通 ...

  3. Android 侧滑菜单的简单实现(SlidingMenu)

    在我还没有学习Android的时候就用过侧滑菜单的APP,当时第一个感觉是:哇塞,这效果不错!当然,现在自己都已经学Android了,这效果当然也要做出来啊~ SlidingMenu是一种比较新的设置 ...

  4. android侧滑菜单笔记

    一.SlidingPaneLayout v4包下的控件,使用简单,功能简洁.官方文档明确说明该控件只能左侧滑动.使用如下: <android.support.v4.widget.SlidingP ...

  5. android 侧滑菜单

    就是用手一滑才出现,占手机半个多屏幕的菜单.为了美观和页面转跳,很多时候要用到. 实现的话就是使用官方的DrawerLayout,注意这个布局一定要是最顶层的布局. 在DrawerLayout里面直接 ...

  6. Android侧滑菜单和轮播图之滑动冲突

    接手一个项目,有一个问题需要修改:轮播图不能手动滑动,手动滑动轮播图只会触发侧滑菜单. 猜测:viewpager控件(轮播图)的触摸事件被SlidingMenu控件(侧滑菜单,非第三方项目,乃是上个开 ...

  7. Android滑动菜单框架完全解析,教你如何一分钟实现滑动菜单特效

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744400 之前我向大家介绍了史上最简单的滑动菜单的实现方式,相信大家都还记得.如 ...

  8. Android组件——使用DrawerLayout仿网易新闻v4.4侧滑菜单

    摘要: 转载请注明出处:http://blog.csdn.net/allen315410/article/details/42914501 概述        今天这篇博客将记录一些关于DrawerL ...

  9. Android之自定义侧滑菜单

    先来上图: 我们把主界面从左向右拉动,可以看到地下有一层菜单页,从透明渐渐变得不透明,从小渐渐变大,感觉上觉得菜单页是从屏幕外面被拉到屏幕中的.下面的代码实现这个DEMO: 首先是自定义控件Slidi ...

随机推荐

  1. 第五周linux学习笔记

    第五章 系统调用 5.1 与内核通信 系统调用在用户空间进程和硬件设备之间添加了一个中间层.该层主要作用有三个. 它为用户空间提供了一种硬件的抽象接口. 系统调用保 证了系统的毡定和安全. 在第 3 ...

  2. linux内核分析 第五周 扒开系统调用的三层皮(下)

    rm menu -rf 强制删除原menu文件 git clone http://github.com/mengning/menu.git 从github中克隆 cd menu 在test.c中增加上 ...

  3. UVA.12716 GCD XOR (暴力枚举 数论GCD)

    UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...

  4. golang 解码未知键的 json 字符串

    我们可以使用 interface 接收 json.Unmarshal 的结果,然后利用 type assertion 特性来进行后续操作. package main import ( "en ...

  5. 三、java面向对象编程_1

    目录 一.对象和类的概念 二.对象和引用 1.对象 2.成员变量 3.引用 三.java类的定义 四.构造函数(构造方法) 五.内存分析 一.对象和类的概念 1.对象 对象用计算机语言对应问题域中事物 ...

  6. Android Studio 安装在Windows10中的陷阱

    操作系统:Windows 10 Pro CPU:AMD IDE:Android Studio 2.0 JDK:8.0 安装完AS(Android Studio)之后,运行AS发现无法启动模拟器,提示“ ...

  7. Java基础-异常(Exception)处理

    Java基础-异常(Exception)处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.异常的概述 什么是异常?Java代码在运行时期发生的问题就是异常.在Java中,把异 ...

  8. bzoj千题计划138:bzoj1432: [ZJOI2009]Function

    http://www.lydsy.com/JudgeOnline/problem.php?id=1432 http://blog.sina.com.cn/s/blog_86942b1401014bd2 ...

  9. 用pip install升级已安装的包的附加包, 以tabulate包为例

    用pip install升级已安装的附加包, 以tabulate包为例 去pypi官网查看tabulate包的介绍, 发现tabulate 0.7.6才开始支持宽字符的美化打印. 而且还需要安装它的附 ...

  10. [转载]WCF和ASP.NET Web API在应用上的选择

    http://www.cnblogs.com/shanyou/archive/2012/09/26/2704814.html http://msdn.microsoft.com/en-us/libra ...