github的地址是:https://github.com/jfeinstein10/SlidingMenu
可以根據上面的setup去做,一步一步,總會成功
下面使用在android studio 的ant編譯環境和eclipse的編譯環境里正常使用,裏面的資源來自SlidingMenu開源項目。
1. checkout上面的sliding library和sample,ActionBarSherlock 的library。
分別爲:
https://github.com/jfeinstein10/SlidingMenu
2. 新建一個項目,導入SlidingMenu 和 ActionBarSherlock 的library,主項目引用SlidingMenu-library和ActionBarSherlock -library兩個工程,
此時 SlidingMenu 報錯,
在SlidingMenu-library找到SlidingFragmentActivity.java,然後把extends FragmentActivity 改爲extends
SherlockFragmentActivity
SlidingMenu-library:
主項目:
3. SlidingMenu-library工程和主項目都要引用support-v4包,然後還有錯誤的話在工程上分別選android版本,基本上就沒問題了。
4. 在主Activity的OnCreate里加入代碼
private void initSlidingMenu() {
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
FourthFragment mFrag = new FourthFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
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);
getSlidingMenu().setMode(SlidingMenu.LEFT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
FourthFragment mFrag = new FourthFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
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);
getSlidingMenu().setMode(SlidingMenu.LEFT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
抽屜的佈局
menu_frame.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
FourthFragment:
隨便用IDE新建一個Fragment即可。
在dimens.xml里加入
<dimen name="slidingmenu_offset">60dp</dimen>
<dimen name="shadow_width">15dp</dimen>
<dimen name="shadow_width">15dp</dimen>
新建一個shadow.xml是用來產生陰影的:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:endColor="#33000000"
android:centerColor="#11000000"
android:startColor="#00000000" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:endColor="#33000000"
android:centerColor="#11000000"
android:startColor="#00000000" />
</shape>
5. 自此,輕輕鬆鬆主項目里加入SlidingMenu效果。
如:A.主界面,B.抽屜界面,C.手指滑動中