Android SlidingMenu使用和示例详解
很多app都有侧滑菜单的功能,部分app左右都是侧滑菜单~slidingmenu 这个开源项目可以很好帮助我们实现侧滑功能,如果对slidingmenu 还不是很了解的童鞋,可以参考下本篇博客。将侧滑菜单引入项目的方式很多中,本博客先通过例子介绍各种引入方式,然后给大家展示个实例:主布局viewpager,左右各一个侧滑菜单的用法,差不多已经能满足大部分应用的需求了。关于常用属性,在文章末尾介绍。
1、在activity中通过slidingmenu构造方法,直接设置侧滑菜单
package com.zhy.zhy_slidemenu_demo; import androidappactivity; import androidosbundle; import comjeremyfeinsteinslidingmenulibslidingmenu; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { superoncreate(savedinstancestate); setcontentview(rlayoutactivity_main); // configure the slidingmenu slidingmenu menu = new slidingmenu(this); menusetmode(slidingmenuleft); // 设置触摸屏幕的模式 menusettouchmodeabove(slidingmenutouchmode_fullscreen); menusetshadowwidthres(rdimenshadow_width); menusetshadowdrawable(rdrawableshadow); // 设置滑动菜单视图的宽度 menusetbehindoffsetres(rdimenslidingmenu_offset); // 设置渐入渐出效果的值 menusetfadedegree(35f); /** * sliding_window will include the title/actionbar in the content * section of the slidingmenu, while sliding_content does not */ menuattachtoactivity(this, slidingmenusliding_content); //为侧滑菜单设置布局 menusetmenu(rlayoutleftmenu); } }
效果图:
是不是特别简单~几行代码搞定~哈~
2、通过把activity继承slidingactivity
a、继承slidingactivity
b、然后在oncreate中setbehindcontentview(r.layout.leftmenu); 设置侧滑菜单的布局
c、通过getslidingmenu()得到slidingmenu对象,然后设置样式
package com.zhy.zhy_slidemenu_demo02; import androidosbundle; import comjeremyfeinsteinslidingmenulibslidingmenu; import comjeremyfeinsteinslidingmenulibappslidingactivity; public class mainactivity extends slidingactivity { @override public void oncreate(bundle savedinstancestate) { superoncreate(savedinstancestate); setcontentview(rlayoutactivity_main); setbehindcontentview(rlayoutleftmenu); // configure the slidingmenu slidingmenu menu = getslidingmenu(); menusetmode(slidingmenuleft); // 设置触摸屏幕的模式 menusettouchmodeabove(slidingmenutouchmode_fullscreen); menusetshadowwidthres(rdimenshadow_width); menusetshadowdrawable(rdrawableshadow); // 设置滑动菜单视图的宽度 menusetbehindoffsetres(rdimenslidingmenu_offset); // 设置渐入渐出效果的值 menusetfadedegree(35f); /** * sliding_window will include the title/actionbar in the content * section of the slidingmenu, while sliding_content does not */ // menuattachtoactivity(this, slidingmenusliding_content); // menusetmenu(rlayoutleftmenu); } }
效果图和第一种方式一样~是不是也很简单~~
3、将slidingmenu当作普通控件
可以把slidingmenu作为普通的view,然后在布局中声明,丧心病狂的玩~下面看个例子:
<relativelayout xmlns:android="http://schemasandroidcom/apk/res/android" xmlns:tools="http://schemasandroidcom/tools" android:id="@+id/id_main_ly" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="30dp" android:layout_margintop="30dp" > <comjeremyfeinsteinslidingmenulibslidingmenu xmlns:sliding="http://schemasandroidcom/apk/res-auto" android:id="@+id/slidingmenulayout" android:layout_width="120dp" android:layout_height="170dp" android:background="#ffffffff" sliding:behindoffset="0dp" sliding:behindscrollscale="1" sliding:fadedegree="3" sliding:fadeenabled="true" sliding:touchmodeabove="fullscreen" sliding:viewabove="@layout/pic" /> </linearlayout> </relativelayout>
我们slidingmenu作为普通view在布局文件中设置了布局,并且设置了viewabove的值为另一个布局。
下面看pic布局:
<?xml version="1.0" encoding="utf-8"?> <imageview xmlns:android="http://schemasandroidcom/apk/res/android" android:layout_width="120dp" android:layout_height="170dp" android:src="@drawable/zhy" />
就是一张妹子图片。
最后看主activity:
package comzhyzhy_slidemenu_demo03; import androidappactivity; import androidosbundle; import androidviewview; import androidviewviewonclicklistener; import comjeremyfeinsteinslidingmenulibslidingmenu; public class mainactivity extends activity { private slidingmenu mleftmenu; @override protected void oncreate(bundle savedinstancestate) { superoncreate(savedinstancestate); setcontentview(rlayoutactivity_main); mleftmenu = (slidingmenu) findviewbyid(ridslidingmenulayout); // configure the slidingmenu // slidingmenu menu = new slidingmenu(this); mleftmenusetmode(slidingmenuleft); // 设置触摸屏幕的模式 mleftmenusetshadowwidthres(rdimenshadow_width); mleftmenusetshadowdrawable(rdrawableshadow); mleftmenusetmenu(rlayoutleftmenu); mleftmenusetonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mleftmenuismenushowing()) mleftmenutoggle(); } }); // 设置滑动菜单视图的宽度 // 设置渐入渐出效果的值 /** * sliding_window will include the title/actionbar in the content * section of the slidingmenu, while sliding_content does not */ } }
效果图:
通过slidingmenu给图片设置一个滑动展示介绍,你也可以滑动显示任何东西,下载、分享按钮什么的。图片很多的时候不知道效率咋样,可以这么玩,但是不建议哈~
4、slidingmenu设置左右侧滑菜单例子
上面介绍的3个方法,slidingmenu的布局中控件的事件都需要写在activity中,这样代码比较臃肿,一般会使用fragment作为侧滑菜单的布局容器。
核心代码:
fragment leftmenufragment = new menuleftfragment(); setbehindcontentview(rlayoutleft_menu_frame); getsupportfragmentmanager()begintransaction() replace(ridid_left_menu_frame, leftmenufragment)commit(); slidingmenu menu = getslidingmenu(); menusetmode(slidingmenuleft_right); // 设置触摸屏幕的模式 menusettouchmodeabove(slidingmenutouchmode_margin);
先给侧滑菜单通过 setbehindcontentview(r.layout.left_menu_frame);设置一个布局,此布局中只有一个framelayout,然后使用fragmentmanager将fragment替换掉此fragment,这样这个fragment就作为我们侧滑菜单的布局了,我们的事件处理代码也可以写在fragement中,而不是activity中。
下面看具体例子:
首先分别是左右两边的两个fragment:
menuleftfragment
package comzhyzhy_slidemenu_demo04; import javautilarrays; import javautillist; import androidosbundle; import androidsupportvappfragment; import androidviewlayoutinflater; import androidviewview; import androidviewviewgroup; import androidwidgetarrayadapter; import androidwidgetlistadapter; import androidwidgetlistview; public class menuleftfragment extends fragment { private view mview; private listview mcategories; private list<string> mdatas = arrays aslist("聊天", "发现", "通讯录", "朋友圈", "订阅号"); private listadapter madapter; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if (mview == null) { initview(inflater, container); } return mview; } private void initview(layoutinflater inflater, viewgroup container) { mview = inflaterinflate(rlayoutleft_menu, container, false); mcategories = (listview) mview findviewbyid(ridid_listview_categories); madapter = new arrayadapter<string>(getactivity(), androidrlayoutsimple_list_item_1, mdatas); mcategoriessetadapter(madapter); } }
左边的侧滑布局就是一个listview,代码比较简单~
menurightfragment
package comzhyzhy_slidemenu_demo04; import androidosbundle; import androidsupportvappfragment; import androidviewlayoutinflater; import androidviewview; import androidviewviewgroup; public class menurightfragment extends fragment { private view mview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if(mview == null) { mview = inflaterinflate(rlayoutright_menu, container, false); } return mview ; } }
右边的侧滑菜单的fragment,加载了一个布局,没有做任何事件处理~
最后是mainactivity
package comzhyzhy_slidemenu_demo04; import javautilarraylist; import javautillist; import androidosbundle; import androidsupportvappfragment; import androidsupportvappfragmentpageradapter; import androidsupportvviewviewpager; import androidviewview; import androidviewwindow; import comjeremyfeinsteinslidingmenulibslidingmenu; import comjeremyfeinsteinslidingmenulibappslidingfragmentactivity; public class mainactivity extends slidingfragmentactivity { private viewpager mviewpager; private fragmentpageradapter madapter; private list<fragment> mfragments = new arraylist<fragment>(); @override public void oncreate(bundle savedinstancestate) { superoncreate(savedinstancestate); thisrequestwindowfeature(windowfeature_no_title); setcontentview(rlayoutactivity_main); // 初始化slidemenu initrightmenu(); // 初始化viewpager initviewpager(); } private void initviewpager() { mviewpager = (viewpager) findviewbyid(ridid_viewpager); maintab01 tab01 = new maintab01(); maintab02 tab02 = new maintab02(); maintab03 tab03 = new maintab03(); mfragmentsadd(tab01); mfragmentsadd(tab02); mfragmentsadd(tab03); /** * 初始化adapter */ madapter = new fragmentpageradapter(getsupportfragmentmanager()) { @override public int getcount() { return mfragmentssize(); } @override public fragment getitem(int arg0) { return mfragmentsget(arg0); } }; mviewpagersetadapter(madapter); } private void initrightmenu() { fragment leftmenufragment = new menuleftfragment(); setbehindcontentview(rlayoutleft_menu_frame); getsupportfragmentmanager()begintransaction() replace(ridid_left_menu_frame, leftmenufragment)commit(); slidingmenu menu = getslidingmenu(); menusetmode(slidingmenuleft_right); // 设置触摸屏幕的模式 menusettouchmodeabove(slidingmenutouchmode_margin); menusetshadowwidthres(rdimenshadow_width); menusetshadowdrawable(rdrawableshadow); // 设置滑动菜单视图的宽度 menusetbehindoffsetres(rdimenslidingmenu_offset); // 设置渐入渐出效果的值 menusetfadedegree(35f); // menusetbehindscrollscale(0f); menusetsecondaryshadowdrawable(rdrawableshadow); //设置右边(二级)侧滑菜单 menusetsecondarymenu(rlayoutright_menu_frame); fragment rightmenufragment = new menurightfragment(); getsupportfragmentmanager()begintransaction() replace(ridid_right_menu_frame, rightmenufragment)commit(); } public void showleftmenu(view view) { getslidingmenu()showmenu(); } public void showrightmenu(view view) { getslidingmenu()showsecondarymenu(); } }
简单说明一下,mainactivity继承的是slidingfragmentactivity ,在activity中fragmentpageradapter和viewpager作为主布局,然后分别初始化slidingmenu的两边的菜单。
效果图:
哈哈,微信又躺枪了~~这个例子应该可以满足一般app的需求了。
5、slidingmenu的一些常用属性
//设置侧滑菜单的位置,可选值left , right , left_right (两边都有菜单时设置) menu.setmode(slidingmenu.left_right); // 设置触摸屏幕的模式,可选只margin , content menu.settouchmodeabove(slidingmenu.touchmode_margin); //根据dimension资源文件的id来设置阴影的宽度 menu.setshadowwidthres(r.dimen.shadow_width); //根据资源文件id来设置滑动菜单的阴影效果 menu.setshadowdrawable(r.drawable.shadow); // 这两个都是设置滑动菜单视图的宽度,二选一 //设置slidingmenu离屏幕的偏移量 menu.setbehindoffsetres(r.dimen.slidingmenu_offset); //设置宽度 menu.setbehindwidth() // 设置渐入渐出效果的值 menu.setfadedegree(0.35f); //设置slidingmenu与下方视图的移动的速度比,当为1时同时移动,取值0-1 menu.setbehindscrollscale(1.0f); //设置二级菜单的阴影效果 menu.setsecondaryshadowdrawable(r.drawable.shadow); //设置右边(二级)侧滑菜单 menu.setsecondarymenu(r.layout.right_menu_frame); //为侧滑菜单设置布局 menu.setmenu(r.layout.leftmenu); //把滑动菜单添加进所有的activity中,可选值sliding_content , sliding_window menu.attachtoactivity(this, slidingmenu.sliding_content);
还有几个监听事件:
/** * 设置打开监听事件,当滑动菜单被打开时调用 */ public void setonopenlistener(onopenlistener listener) { mopenlistener = listener; } /** * 设置关闭监听事件,当滑动菜单被关闭时调用 */ public void setoncloselistener(oncloselistener listener) { //mviewabovesetoncloselistener(listener); mcloselistener = listener; } /** * 设置打开监听事件,当滑动菜单被打开过之后调用 */ public void setonopenedlistener(onopenedlistener listener) { mviewabovesetonopenedlistener(listener); } /** * 设置关闭监听事件,当滑动菜单被关闭过之后调用 */ public void setonclosedlistener(onclosedlistener listener) { mviewabovesetonclosedlistener(listener); }
还有一些用到的时候查查把~
我把上面的所有例子包括slidingmenu的lib打了一个包~:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java加密算法分享(rsa解密、对称加密、md5加密)
下一篇: 30天搞定Java--第8天
推荐阅读