SlidingMenu是一个优秀的开源项目,可以实现侧滑菜单,简单介绍一下这SlidingMenu的使用:
常用属性和方法:
setTouchModeAbove(int i )是否可以通过滑动手势打开menu
TOUCHMODE_MARGIN = 0 在边缘滑动
TOUCHMODE_FULLSCREEN = 1 在屏幕任意位置滑动
TOUCHMODE_NONE = 2 滑动无法打开menu
setMode(int mode)设置menu出现位置
LEFT = 0 菜单出现在屏幕左侧
RIGHT = 1 菜单出现在屏幕右侧
LEFT_RIGHT = 2 菜单左右都可以划出
attachToActivity(Activity activity, int slideStyle)将menu附加到activity上
SLIDING_WINDOW menu布满全屏(含标题)
SLIDING_CONTENT menu布满全文(不含标题)
setBehindOffsetRes(int resID)设置菜单拉出之后,屏幕剩余的尺寸
setMenu(int res)为菜单设置布局
例子:
准备工作:
2.下载SlidingMenu的依赖组件ActionBarSherlock
3.导入SlidingMenu和ActionBarSherlock
4.项目引入SlidingMenu和ActionBarSherlock
MainActivity.java
package cn.lixyz.slidingmenu; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { private SlidingMenu slidingMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); slidingMenu = new SlidingMenu(this); slidingMenu.setMode(SlidingMenu.LEFT); slidingMenu.setBehindOffsetRes(R.dimen.sliding_menu_offset); slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); // 为侧滑菜单设置布局 slidingMenu.setMenu(R.layout.sliding_mune); } }
sliding_mune.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu android:id="@+id/slidingmenulayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置" > </Button> </com.jeremyfeinstein.slidingmenu.lib.SlidingMenu> </LinearLayout>
dimens.xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="sliding_menu_offset">150dp</dimen> </resources>
运行效果
时间: 2024-12-15 15:51:38