#开源库地址
https://github.com/jfeinstein10/SlidingMenu
下载开源库
SlidingMenu-master 导入 library
ActionBarSherlock-master 导入actionbarsherlock
运行项目会报错
Found 3 versions of android-support-v4.jar in the dependency list,
but not all the versions are identical (check is based on SHA-1 only at this time).
All versions of the libraries must be the same at this time.
但是由于在自己的项目中的v4包的版本不同,所以要把自己项目中的v4包放到导入的包中.
MainActivity中
public class MainActivity extends Activity {
private SlidingMenu menu;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu = new SlidingMenu(this);
//设置侧滑的方向
menu.setMode(SlidingMenu.LEFT);
menu.setShadowWidthRes(R.dimen.shadow_width);
//设置菜单的宽度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//设置为全屏都可以触摸
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//加载侧滑页面
menu.setMenu(R.layout.menu);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//弹出菜单的动作
menu.toggle(true);
}
});
}
//设置点击菜单按钮时弹出菜单栏
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
menu.toggle(true);
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
#在activity_main中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="菜单"/>
</RelativeLayout>
#在menu activity中
<?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"
android:background="#ff666666">
<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">
<Button
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="菜单"/>
</com.jeremyfeinstein.slidingmenu.lib.SlidingMenu>
</LinearLayout>