slidingmenu是一个非常流行的开源组件,可以实现侧滑菜单的效果.
下面来讲解下怎么使用,集成的方式有3种方式.
1. 通过编程的方式把Activity包装进SlidingMenu里,如下所示:
menu = new SlidingMenu(this); // 设置SlidingMenu各种属性 menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); menu.setShadowWidthRes(R.dimen.shadow_width); menu.setShadowDrawable(R.drawable.shadow); menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); menu.setFadeDegree(0.35f); //把Activity附着到SlidingMenu menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); //设置侧边的菜单的layout menu.setMenu(R.layout.menu_frame);
public class BaseActivity extends SlidingFragmentActivity{ //... public void onCreate(Bundle savedInstanceState) { //.... // customize the SlidingMenu SlidingMenu sm = getSlidingMenu(); //通过父类的getSlidingMenu得到SlidingMenu sm.setShadowWidthRes(R.dimen.shadow_width); sm.setShadowDrawable(R.drawable.shadow); sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); sm.setFadeDegree(0.35f); sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); //... }
<com.jeremyfeinstein.slidingmenu.lib.SlidingMenu xmlns:sliding="http://schemas.android.com/apk/res-auto" android:id="@+id/slidingmenulayout" android:layout_width="fill_parent" android:layout_height="fill_parent" sliding:viewAbove="@layout/YOUR_ABOVE_VIEW" <!--这里放上层的内容视图--> sliding:viewBehind="@layout/YOUR_BEHIND_BEHIND" <!--这里放底下的Menu视图--> sliding:touchModeAbove="margin|fullscreen" <!--只在边缘滑动时调出菜单,还是全屏滑动时都可以调出菜单--> sliding:behindOffset="@dimen/YOUR_OFFSET" sliding:behindWidth="@dimen/YOUR_WIDTH" sliding:behindScrollScale="@dimen/YOUR_SCALE" sliding:shadowDrawable="@drawable/YOUR_SHADOW" sliding:shadowWidth="@dimen/YOUR_SHADOW_WIDTH" sliding:fadeEnabled="true|false" sliding:fadeDegree="float" sliding:selectorEnabled="true|false" sliding:selectorDrawable="@drawable/YOUR_SELECTOR"/>
好的title得到要启动的Activity的类,然后把它包装在Intent中,最后startActivity启动这个Activity.
Class<?> cls = null; String title = pref.getTitle().toString(); if (title.equals(getString(R.string.properties))) { cls = PropertiesActivity.class; } else if (title.equals(getString(R.string.attach))) { cls = AttachExample.class; Intent intent = new Intent(this, cls); startActivity(intent); return true;