SlidingMenu的简单使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(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>


源码下载链接:slidingMenu-Android代码类资源-CSDN下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值