随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
直接上代码:
MainActivity:
package com.home.testslidingmenu;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.Window;
import com.home.textslidingmenu.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class MainActivity extends SlidingFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
initSlidingMenu();
}
/**
* 初始化SlidingMenu
*/
private void initSlidingMenu() {
Fragment leftMenuFragment = new LeftFragment();
setBehindContentView(R.layout.left_menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.left_menu_frame_rootlayout, leftMenuFragment)
.commit();
SlidingMenu menu = getSlidingMenu();
//左右侧滑
menu.setMode(SlidingMenu.LEFT_RIGHT);
//左侧滑
// menu.setMode(SlidingMenu.LEFT);
//右侧滑
// menu.setMode(SlidingMenu.RIGHT);
// 设置触摸屏幕的模式:边角触摸
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
// 设置触摸屏幕的模式:全屏触摸
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//设置阴影宽度
menu.setShadowWidthRes(R.dimen.shadow_width);
//设置阴影效果
menu.setShadowDrawable(R.drawable.shadow);
// 设置滑动菜单视图的宽度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// menu.setBehindWidth(0);
// 设置渐入渐出效果的值
menu.setFadeDegree(0.35f);
// menu.setBehindScrollScale(1.0f);
/**
* SLIDING_WINDOW will include the Title/ActionBar in the content
* section of the SlidingMenu, while SLIDING_CONTENT does not.
*/
//把滑动菜单添加进所有的Activity中,可选值SLIDING_CONTENT , SLIDING_WINDOW
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
//设置右边菜单阴影效果
menu.setSecondaryShadowDrawable(R.drawable.shadow);
// 设置右边(二级)侧滑菜单
menu.setSecondaryMenu(R.layout.right_menu_frame);
Fragment rightMenuFragment = new RightFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.right_menu_frame_rootlayout, rightMenuFragment)
.commit();
}
public void showLeftMenu(View view) {
getSlidingMenu().showMenu();
}
public void showRightMenu(View view) {
getSlidingMenu().showSecondaryMenu();
}
}
LeftFragment:
package com.home.testslidingmenu;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.home.textslidingmenu.R;
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_menu, container, false);
}
}
RightFragment:
package com.home.testslidingmenu;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.home.textslidingmenu.R;
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_menu, container, false);
}
}
drawable里面的left_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/left_normal" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/left_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/left_pressed" android:state_focused="true"/>
</selector>
drawable里面的right_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/right_normal" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/right_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/right_pressed" android:state_focused="true"/>
</selector>
drawable里面的shadow.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:endColor="#ff444444"
android:startColor="#00000000" />
</shape>
布局文件:
main.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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/left_selector"
android:onClick="showLeftMenu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="测试SlidingMenu"
android:textColor="#fff"
android:textSize="25sp"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:background="@drawable/right_selector"
android:onClick="showRightMenu" />
</RelativeLayout>
</LinearLayout>
left_menu.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" >
<TextView
android:layout_width="match_parent"
android:text="左边布局"
android:gravity="center"
android:textSize="40sp"
android:layout_height="match_parent"/>
</LinearLayout>
right_menu.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="右边布局"
android:textSize="40sp"
android:gravity="center"/>
</LinearLayout>
left_menu_frame.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left_menu_frame_rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
right_menu_frame.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right_menu_frame_rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
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="slidingmenu_offset">60dp</dimen>
<dimen name="shadow_width">5dp</dimen>
</resources>