SlidingMenu的简单使用

直接上代码:

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 {@Overridepublic 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);//设置右边菜单阴影效果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 {@Overridepublic 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 {@Overridepublic 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=""><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=""><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="" ><gradientandroid:endColor="#ff444444"android:startColor="#00000000" /></shape>布局文件:main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/title_bar" ><ImageViewandroid: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" /><TextViewandroid: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" /><ImageViewandroid: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=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:text="左边布局"android:gravity="center"android:textSize="40sp"android:layout_height="match_parent"/></LinearLayout>

right_menu.xml:其实每一朵花,都有它自己的生命,

SlidingMenu的简单使用

相关文章:

你感兴趣的文章:

标签云: