怎么使用侧滑菜单slidingmenu菜单点击

怎么使用侧滑菜单SlidingMenu_百度知道
怎么使用侧滑菜单SlidingMenu
提问者采纳
 看一下怎么使用吧!  /** SlidingMenu 常用属性介绍: */menu.setMode(SlidingMenu.LEFT);//设置左滑菜单 SlidingMenu.RIGHT
SlidingMenu.LEFT_RIGHT 右侧滑/左右侧滑menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置滑动的屏幕局限,该设置为全屏区域都可以滑动menu.setShadowDrawable(R.drawable.shadow);//设置暗影menu.setShadowWidthRes(R.dimen.shadow_width);//设置暗影的宽度menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu划出时主页面显示的残剩宽度menu.setBehindWidth(400);//设置SlidingMenu菜单的宽度menu.setFadeDegree(0.35f);//SlidingMenu滑动时的渐变程度menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//使SlidingMenu附加在Activity上menu.setMenu(R.layout.menu_layout);//设置menu的布局文件menu.toggle();//动态断定主动封闭或开启SlidingMenumenu.showMenu();//显示SlidingMenumenu.showContent();//显示内容menu.setOnOpenListener(onOpenListener);//slidingmenu打开关于封闭menu有两个,简单的来说,对于menu close事务,一个是when,一个是after menu.OnClosedListener(OnClosedListener);//slidingmenu封闭时事务menu.OnClosedListener(OnClosedListener);//slidingmenu封闭后事务  简单设置左方向菜单侧滑的使用方法:  public class SlidingExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.attach);
// set the content view
setContentView(R.layout.content);
// configure the SlidingMenu
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.menu); }}  如果你要设置左右菜单侧滑,看这里:  public class MainActivity extends FragmentActivity { public SlidingMenu mSlidingM @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSlidingMenu = new SlidingMenu(this);
mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
mSlidingMenu.setMenu(R.layout.slidemenu_primary);//左侧滑
mSlidingMenu.setSecondaryMenu(R.layout.slidemenu_secondery);//右侧滑getSupportFragmentManager().beginTransaction().
replace(R.id.aty_main_framLayout, new ViewPagerFragment()).commit();getSupportFragmentManager().beginTransaction().
replace(R.id.framLayout_fragment, new TranslationAnimationFragment()).commit();}/**
重写返回键和menu键,控制菜单侧滑关闭
*/ @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(mSlidingMenu.isMenuShowing())
mSlidingMenu.showContent();
else finish();
case KeyEvent.KEYCODE_MENU:
mSlidingMenu.showSecondaryMenu();
}return super.onKeyDown(keyCode, event); }}  下面看一下如何解决侧滑跟ViewPager左右滑动的冲突:《当然,我的源码上展示了怎么使用》  mSlidingMenu.addIgnoredView(viewPager);  在你的Viewpager内容显示之前,调用上面的方法,就Ok了、  SlidingMenu.setTouchModeAbove().其中一共包含三中手势模式:TOUCHMODE_FULLSCREEN 全屏模式,在正文布局中通过手势也可以打开SlidingMenuTOUCHMODE_MARGIN 边缘模式,在正文布局的边缘处通过手势可以找开SlidingMenuTOUCHMODE_NONE 自然是不能通过手势打开SlidingMenu了  当你在设置暗影时,在res/drawable/shadow.xml  &?xml version=&1.0& encoding=&utf-8&?&&shape xmlns:android=&& & &gradient
android:centerColor=&#&
android:endColor=&#&
android:startColor=&#& /&&/shape&  转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦
来自团队:
其他类似问题
为您推荐:
侧滑的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁【android开发】滑动菜单栏(开源项目SlidingMenu的示范)( 上)_开源软件大全_优良自学吧 |
当前位置: >
> 【android开发】滑动菜单栏(开源项目SlidingMenu的示范)( 上)优良自学吧提供【android开发】滑动菜单栏(开源项目SlidingMenu的示范)( 上),【android开发】滑动菜单栏(开源项目SlidingMenu的示例)( 上)随着android界面不断的优化,滑动菜单栏越来越受到使用者的喜欢,很多软件都采用了这种滑动效果,一方面操作方便,另一方面给本身狭小的手机屏幕节省了不少的空间。很多优秀的应用都采用了这种界面方案,像facebook、人人网、eve【android开发】滑动菜单栏(开源项目SlidingMenu的示例)( 上)随着android界面不断的优化,滑动菜单栏越来越受到使用者的喜欢,很多软件都采用了这种滑动效果,一方面操作方便,另一方面给本身狭小的手机屏幕节省了不少的空间。很多优秀的应用都采用了这种界面方案,像facebook、人人网、everynote、Google+、360手机助手等。
& & & & & &360手机助手界面 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &Google+界面
& & & & & & & & & &
效果看起来确实比较新颖,所以在很多的应用开发中去实现此效果,解决的办法也是不尽相同。诸多比较以后发现,还是GitHub上的开源项目SlidingMenu提供了最佳的实现:定制灵活、各种阴影和渐变以及动画的滑动效果都很不错。不过这是一个开源库,而不是一个完整的项目,要把它作为libary引入到你自己的工程里,简单配置一下就可以实现SlidingMenu的效果。
这篇博文主要就是介绍一下SlidingMenu的使用方法和主要类的代码。
要想能够实现SlidingMenu的效果,首先必须要将它作为libary导入到你的工程,先看一下它的目录结构,如图:
下面我们主要介绍一下开源库SlidingMenu类,这个类是我们在实现滑动菜单时,要调用的,包括设置菜单页面,菜单的属性等,都是通过这个类里面的方法来实现的。至于怎么获取焦点、判断焦点位置等,这些也是被封装好了,这个相对复杂一些,有兴趣的可以看看这个库。很多朋友认为,不需要了解这个开源库,直接拿来就行了,个人感觉这对于一个在不断进步的人来说,是不妥的!所以,了解他们实现的过程和方法是很有必要,可以独立写不出来,但是需要我们知道他实现的过程和原理。库中的类,主要的都加了必要的注释,对于大家了解,有所帮助,感兴趣的可以下载看看,最后会把下载链接告诉大家。那么现在我们就看看SlidingMenu类吧!
package com.lxh.slidingmenu.
import java.lang.reflect.M
import android.annotation.SuppressL
import android.annotation.TargetA
import android.app.A
import android.content.C
import android.content.res.TypedA
import android.graphics.B
import android.graphics.BitmapF
import android.graphics.C
import android.graphics.P
import android.graphics.R
import android.graphics.drawable.D
import android.os.B
import android.os.H
import android.os.P
import android.os.P
import android.util.AttributeS
import android.util.L
import android.view.D
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.view.WindowM
import android.widget.FrameL
import android.widget.RelativeL
import com.jeremyfeinstein.slidingmenu.lib.R;
import com.lxh.slidingmenu.lib.CustomViewAbove.OnPageChangeL
public class SlidingMenu extends RelativeLayout {
private static final String TAG = &SlidingMenu&;
public static final int SLIDING_WINDOW = 0;
public static final int SLIDING_CONTENT = 1;
private boolean mActionbarOverlay =
* 为setTouchModeAbove()方法设置一个常量值,允许滑动菜单通过滑动屏幕的边缘被打开
public static final int TOUCHMODE_MARGIN = 0;
* 为setTouchModeAbove()方法设置一个常量值,允许滑动菜单通过滑动屏幕的任何地方被打开
public static final int TOUCHMODE_FULLSCREEN = 1;
* 为setTouchModeAbove()方法设置一个常量值,不允许滑动菜单通过滑动屏幕被打开
public static final int TOUCHMODE_NONE = 2;
* 为setMode()方法设置一个常量值,把滑动菜单放在左边
public static final int LEFT = 0;
* 为setMode()方法设置一个常量值,把滑动菜单放在右边
public static final int RIGHT = 1;
* 为setMode()方法设置一个常量值,把滑动菜单放在左右两边
public static final int LEFT_RIGHT = 2;
* 定义上方视图对象
private CustomViewAbove mViewA
* 定义下方视图对象
private CustomViewBehind mViewB
* 定义滑动菜单打开的监听对象
private OnOpenListener mOpenL
* 定义滑动菜单关闭的监听对象
private OnCloseListener mCloseL
* 滑动菜单打开时的监听事件
public interface OnOpenListener {
public void onOpen();
* 监测滑动菜单是否已经打开的监听事件
public interface OnOpenedListener {
public void onOpened();
* 滑动菜单关闭时的监听事件
public interface OnCloseListener {
public void onClose();
* 监测滑动菜单是否已经关闭的监听事件
public interface OnClosedListener {
public void onClosed();
* The Interface CanvasTransformer.
public interface CanvasTransformer {
* Transform canvas.
* @param canvas the canvas
* @param percentOpen the percent open
public void transformCanvas(Canvas canvas, float percentOpen);
* 初始化滑动菜单
* @param context the associated Context
public SlidingMenu(Context context) {
this(context, null);
* 初始化滑动菜单
* @param activity the activity to attach slidingmenu
* @param slideStyle the slidingmenu style
public SlidingMenu(Activity activity, int slideStyle) {
this(activity, null);
this.attachToActivity(activity, slideStyle);
* 初始化滑动菜单
* @param context the associated Context
* @param attrs the attrs
public SlidingMenu(Context context, AttributeSet attrs) {
this(context, attrs, 0);
* 初始化滑动菜单
* @param context the associated Context
* @param attrs the attrs
* @param defStyle the def style
public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutParams behindParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mViewBehind = new CustomViewBehind(context);
addView(mViewBehind, behindParams);
LayoutParams aboveParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mViewAbove = new CustomViewAbove(context);
addView(mViewAbove, aboveParams);
mViewAbove.setCustomViewBehind(mViewBehind);
mViewBehind.setCustomViewAbove(mViewAbove);
mViewAbove.setOnPageChangeListener(new OnPageChangeListener() {
public static final int POSITION_OPEN = 0;
public static final int POSITION_CLOSE = 1;
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) { }
public void onPageSelected(int position) {
if (position == POSITION_OPEN && mOpenListener != null) {
mOpenListener.onOpen();
} else if (position == POSITION_CLOSE && mCloseListener != null) {
mCloseListener.onClose();
// now style everything!
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu);
// set the above and behind views if defined in xml
int mode = ta.getInt(R.styleable.SlidingMenu_mode, LEFT);
setMode(mode);
int viewAbove = ta.getResourceId(R.styleable.SlidingMenu_viewAbove, -1);
if (viewAbove != -1) {
setContent(viewAbove);
setContent(new FrameLayout(context));
int viewBehind = ta.getResourceId(R.styleable.SlidingMenu_viewBehind, -1);
if (viewBehind != -1) {
setMenu(viewBehind);
setMenu(new FrameLayout(context));
int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_touchModeAbove, TOUCHMODE_MARGIN);
setTouchModeAbove(touchModeAbove);
int touchModeBehind = ta.getInt(R.styleable.SlidingMenu_touchModeBehind, TOUCHMODE_MARGIN);
setTouchModeBehind(touchModeBehind);
int offsetBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindOffset, -1);
int widthBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindWidth, -1);
if (offsetBehind != -1 && widthBehind != -1)
throw new IllegalStateException(&Cannot set both behindOffset and behindWidth for a SlidingMenu&);
else if (offsetBehind != -1)
setBehindOffset(offsetBehind);
else if (widthBehind != -1)
setBehindWidth(widthBehind);
setBehindOffset(0);
float scrollOffsetBehind = ta.getFloat(R.styleable.SlidingMenu_behindScrollScale, 0.33f);
setBehindScrollScale(scrollOffsetBehind);
int shadowRes = ta.getResourceId(R.styleable.SlidingMenu_shadowDrawable, -1);
if (shadowRes != -1) {
setShadowDrawable(shadowRes);
int shadowWidth = (int) ta.getDimension(R.styleable.SlidingMenu_shadowWidth, 0);
setShadowWidth(shadowWidth);
boolean fadeEnabled = ta.getBoolean(R.styleable.SlidingMenu_fadeEnabled, true);
setFadeEnabled(fadeEnabled);
float fadeDeg = ta.getFloat(R.styleable.SlidingMenu_fadeDegree, 0.33f);
setFadeDegree(fadeDeg);
boolean selectorEnabled = ta.getBoolean(R.styleable.SlidingMenu_selectorEnabled, false);
setSelectorEnabled(selectorEnabled);
int selectorRes = ta.getResourceId(R.styleable.SlidingMenu_selectorDrawable, -1);
if (selectorRes != -1)
setSelectorDrawable(selectorRes);
ta.recycle();
* 把滑动菜单添加进所有的Activity中
* @param activity the Activity
* @param slideStyle either SLIDING_CONTENT or SLIDING_WINDOW
public void attachToActivity(Activity activity, int slideStyle) {
attachToActivity(activity, slideStyle, false);
* 把滑动菜单添加进所有的Activity中
* @param activity the Activity
* @param slideStyle either SLIDING_CONTENT or SLIDING_WINDOW
* @param actionbarOverlay whether or not the ActionBar is overlaid
public void attachToActivity(Activity activity, int slideStyle, boolean actionbarOverlay) {
if (slideStyle != SLIDING_WINDOW && slideStyle != SLIDING_CONTENT)
throw new IllegalArgumentException(&slideStyle must be either SLIDING_WINDOW or SLIDING_CONTENT&);
if (getParent() != null)
throw new IllegalStateException(&This SlidingMenu appears to already be attached&);
// get the window background
TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground});
int background = a.getResourceId(0, 0);
a.recycle();
switch (slideStyle) {
case SLIDING_WINDOW:
mActionbarOverlay =
ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
// save ActionBar themes that have transparent assets
decorChild.setBackgroundResource(background);
decor.removeView(decorChild);
decor.addView(this);
setContent(decorChild);
case SLIDING_CONTENT:
mActionbarOverlay = actionbarO
// take the above view out of
ViewGroup contentParent = (ViewGroup)activity.findViewById(android.R.id.content);
View content = contentParent.getChildAt(0);
contentParent.removeView(content);
contentParent.addView(this);
setContent(content);
// save people from having transparent backgrounds
if (content.getBackground() == null)
content.setBackgroundResource(background);
* 从布局资源文件中设置上方(左边滑动菜单)的视图内容,这个布局会被填充添加到所有图层的最上方
public void setContent(int res) {
setContent(LayoutInflater.from(getContext()).inflate(res, null));
* 通过View来设置上方(左边滑动菜单)的视图内容
public void setContent(View view) {
mViewAbove.setContent(view);
showContent();
* 得到上方(左边滑动菜单)的视图内容
public View getContent() {
return mViewAbove.getContent();
* 从布局资源文件中设置下方(滑动菜单)的视图内容,这个布局会被填充添加到所有图层的最下方
* @param res the new content
public void setMenu(int res) {
setMenu(LayoutInflater.from(getContext()).inflate(res, null));
* 得到下方(滑动菜单)的视图内容
* @param view The desired content to display.
public void setMenu(View v) {
mViewBehind.setContent(v);
* 得到下方(右边滑动菜单)的视图内容
public View getMenu() {
return mViewBehind.getContent();
* 从布局资源文件中设置下方(右边滑动菜单)的视图内容,这个布局会被填充添加到所有图层的最下方
public void setSecondaryMenu(int res) {
setSecondaryMenu(LayoutInflater.from(getContext()).inflate(res, null));
* 设置下方(右边滑动菜单)的视图内容
public void setSecondaryMenu(View v) {
mViewBehind.setSecondaryContent(v);
* 得到下方(右边滑动菜单)的视图内容
public View getSecondaryMenu() {
return mViewBehind.getSecondaryContent();
* 设置上方视图是否能够滑动
public void setSlidingEnabled(boolean b) {
mViewAbove.setSlidingEnabled(b);
* 检测上方视图是否能够滑动
public boolean isSlidingEnabled() {
return mViewAbove.isSlidingEnabled();
* 设置滑动菜单出现在视图中的位置
* @param mode must be either SlidingMenu.LEFT or SlidingMenu.RIGHT
public void setMode(int mode) {
if (mode != LEFT && mode != RIGHT && mode != LEFT_RIGHT) {
throw new IllegalStateException(&SlidingMenu mode must be LEFT, RIGHT, or LEFT_RIGHT&);
mViewBehind.setMode(mode);
* 得到滑动菜单在视图中的位置
* @return the current mode, either SlidingMenu.LEFT or SlidingMenu.RIGHT
public int getMode() {
return mViewBehind.getMode();
* 设置滑动菜单是否是静态模式(不能够使用滑动菜单)
public void setStatic(boolean b) {
setSlidingEnabled(false);
mViewAbove.setCustomViewBehind(null);
mViewAbove.setCurrentItem(1);
mViewBehind.setCurrentItem(0);
mViewAbove.setCurrentItem(1);
mViewBehind.setCurrentItem(1);
mViewAbove.setCustomViewBehind(mViewBehind);
setSlidingEnabled(true);
* 打开滑动菜单并显示菜单的视图
public void showMenu() {
showMenu(true);
* 是否使用动画效果打开滑动菜单并显示菜单的视图
public void showMenu(boolean animate) {
mViewAbove.setCurrentItem(0, animate);
* 打开右边的滑动菜单并显示菜单的视图
public void showSecondaryMenu() {
showSecondaryMenu(true);
* 是否使用动画效果打开右边的滑动菜单并显示菜单的视图
public void showSecondaryMenu(boolean animate) {
mViewAbove.setCurrentItem(2, animate);
* 关闭菜单并显示上方的视图
public void showContent() {
showContent(true);
* 是否使用动画效果关闭菜单并显示上方的视图
public void showContent(boolean animate) {
mViewAbove.setCurrentItem(1, animate);
* 滑动菜单的开关
public void toggle() {
toggle(true);
* 是否使用动画效果打开或关闭滑动菜单
public void toggle(boolean animate) {
if (isMenuShowing()) {
showContent(animate);
showMenu(animate);
* 检测滑动菜单是否正在被显示
public boolean isMenuShowing() {
return mViewAbove.getCurrentItem() == 0 || mViewAbove.getCurrentItem() == 2;
* 检测右边滑动菜单是否正在被显示
public boolean isSecondaryMenuShowing() {
return mViewAbove.getCurrentItem() == 2;
* 得到下方视图的偏移量
public int getBehindOffset() {
return ((RelativeLayout.LayoutParams)mViewBehind.getLayoutParams()).rightM
* 根据像素的值来设置下方视图的偏移量
* @param i The margin, in pixels, on the right of the screen that the behind view scrolls to.
public void setBehindOffset(int i) {
mViewBehind.setWidthOffset(i);
* 根据dimension资源文件的ID来设置下方视图的偏移量
* @param resID The dimension resource id to be set as the behind offset.
* The menu, when open, will leave this width margin on the right of the screen.
public void setBehindOffsetRes(int resID) {
int i = (int) getContext().getResources().getDimension(resID);
setBehindOffset(i);
* 根据像素的值来设置上方视图的偏移量
* @param i the new above offset, in pixels
public void setAboveOffset(int i) {
mViewAbove.setAboveOffset(i);
* 根据dimension资源文件的ID来设置上方视图的偏移量
* @param resID The dimension resource id to be set as the above offset.
public void setAboveOffsetRes(int resID) {
int i = (int) getContext().getResources().getDimension(resID);
setAboveOffset(i);
* 根据像素的值来设置下方视图的宽度
* @param i The width the Sliding Menu will open to, in pixels
public void setBehindWidth(int i) {
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
Class&?& cls = Display.
Class&?&[] parameterTypes = {Point.class};
Point parameter = new Point();
Method method = cls.getMethod(&getSize&, parameterTypes);
method.invoke(display, parameter);
width = parameter.x;
} catch (Exception e) {
width = display.getWidth();
setBehindOffset(width-i);
* 根据dimension资源文件的ID来设置下方视图的宽度
* @param res The dimension resource id to be set as the behind width offset.
* The menu, when open, will open this wide.
public void setBehindWidthRes(int res) {
int i = (int) getContext().getResources().getDimension(res);
setBehindWidth(i);
* 得到下方视图的在滚动时的缩放比例
* @return The scale of the parallax scroll
public float getBehindScrollScale() {
return mViewBehind.getScrollScale();
* 设置下方视图的在滚动时的缩放比例
* @param f The scale of the parallax scroll (i.e. 1.0f scrolls 1 pixel for every
* 1 pixel that the above view scrolls and 0.0f scrolls 0 pixels)
public void setBehindScrollScale(float f) {
if (f & 0 && f & 1)
throw new IllegalStateException(&ScrollScale must be between 0 and 1&);
mViewBehind.setScrollScale(f);
* 得到边缘触摸的临界值
public int getTouchmodeMarginThreshold() {
return mViewBehind.getMarginThreshold();
* 当触摸的的模式为边缘触摸时,设置边缘触摸的临界值
public void setTouchmodeMarginThreshold(int touchmodeMarginThreshold) {
mViewBehind.setMarginThreshold(touchmodeMarginThreshold);
* Sets the behind canvas transformer.
* @param t the new behind canvas transformer
public void setBehindCanvasTransformer(CanvasTransformer t) {
mViewBehind.setCanvasTransformer(t);
* 得到上方视图的触摸模式的值
public int getTouchModeAbove() {
return mViewAbove.getTouchMode();
* 设置上方视图的触摸模式的值
public void setTouchModeAbove(int i) {
if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN
&& i != TOUCHMODE_NONE) {
throw new IllegalStateException(&TouchMode must be set to either& +
&TOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN or TOUCHMODE_NONE.&);
mViewAbove.setTouchMode(i);
* 设置下方视图的触摸模式的值
public void setTouchModeBehind(int i) {
if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN
&& i != TOUCHMODE_NONE) {
throw new IllegalStateException(&TouchMode must be set to either& +
&TOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN or TOUCHMODE_NONE.&);
mViewBehind.setTouchMode(i);
* 根据资源文件ID来设置滑动菜单的阴影效果
* @param resId the resource ID of the new shadow drawable
public void setShadowDrawable(int resId) {
setShadowDrawable(getContext().getResources().getDrawable(resId));
* 根据Drawable来设置滑动菜单的阴影效果
* @param d the new shadow drawable
public void setShadowDrawable(Drawable d) {
mViewBehind.setShadowDrawable(d);
* 根据资源文件ID来设置右边滑动菜单的阴影效果
* @param resId the resource ID of the new shadow drawable
public void setSecondaryShadowDrawable(int resId) {
setSecondaryShadowDrawable(getContext().getResources().getDrawable(resId));
* 根据Drawable来设置滑动菜单的阴影效果
* @param d the new shadow drawable
public void setSecondaryShadowDrawable(Drawable d) {
mViewBehind.setSecondaryShadowDrawable(d);
* 根据dimension资源文件的ID来设置阴影的宽度
* @param resId The dimension resource id to be set as the shadow width.
public void setShadowWidthRes(int resId) {
setShadowWidth((int)getResources().getDimension(resId));
* 根据像素的值来设置阴影的宽度
* @param pixels the new shadow width, in pixels
public void setShadowWidth(int pixels) {
mViewBehind.setShadowWidth(pixels);
* 设置是否能够使用滑动菜单渐入渐出的效果
public void setFadeEnabled(boolean b) {
mViewBehind.setFadeEnabled(b);
* 设置渐入渐出效果的值
* @param f the new fade degree, between 0.0f and 1.0f
public void setFadeDegree(float f) {
mViewBehind.setFadeDegree(f);
* Enables or disables whether the selector is drawn
* @param b true to draw the selector, false to not draw the selector
public void setSelectorEnabled(boolean b) {
mViewBehind.setSelectorEnabled(true);
* Sets the selected view. The selector will be drawn here
* @param v the new selected view
public void setSelectedView(View v) {
mViewBehind.setSelectedView(v);
* Sets the selector drawable.
* @param res a resource ID for the selector drawable
public void setSelectorDrawable(int res) {
mViewBehind.setSelectorBitmap(BitmapFactory.decodeResource(getResources(), res));
* Sets the selector drawable.
* @param b the new selector bitmap
public void setSelectorBitmap(Bitmap b) {
mViewBehind.setSelectorBitmap(b);
* 添加被忽略的视图
public void addIgnoredView(View v) {
mViewAbove.addIgnoredView(v);
* 移除被忽略的视图
public void removeIgnoredView(View v) {
mViewAbove.removeIgnoredView(v);
* 当模式为Fullscreen模式时,触摸屏幕清除所有被忽略的视图
public void clearIgnoredViews() {
mViewAbove.clearIgnoredViews();
* 设置打开监听事件,当滑动菜单被打开时调用
public void setOnOpenListener(OnOpenListener listener) {
mOpenListener =
* 设置关闭监听事件,当滑动菜单被关闭时调用
public void setOnCloseListener(OnCloseListener listener) {
//mViewAbove.setOnCloseListener(listener);
mCloseListener =
* 设置打开监听事件,当滑动菜单被打开过之后调用
public void setOnOpenedListener(OnOpenedListener listener) {
mViewAbove.setOnOpenedListener(listener);
* 设置关闭监听事件,当滑动菜单被关闭过之后调用
public void setOnClosedListener(OnClosedListener listener) {
mViewAbove.setOnClosedListener(listener);
* 功能描述:保存状态的类,继承自BaseSavedState
public static class SavedState extends BaseSavedState {
private final int mI
public SavedState(Parcelable superState, int item) {
super(superState);
private SavedState(Parcel in) {
super(in);
mItem = in.readInt();
public int getItem() {
/* (non-Javadoc)
* @see android.view.AbsSavedState#writeToParcel(android.os.Parcel, int)
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(mItem);
public static final Parcelable.Creator&SavedState& CREATOR =
new Parcelable.Creator&SavedState&() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
public SavedState[] newArray(int size) {
return new SavedState[size];
/* (non-Javadoc)
* @see android.view.View#onSaveInstanceState()
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState, mViewAbove.getCurrentItem());
/* (non-Javadoc)
* @see android.view.View#onRestoreInstanceState(android.os.Parcelable)
protected void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState)
super.onRestoreInstanceState(ss.getSuperState());
mViewAbove.setCurrentItem(ss.getItem());
/* (non-Javadoc)
* @see android.view.ViewGroup#fitSystemWindows(android.graphics.Rect)
@SuppressLint(&NewApi&)
protected boolean fitSystemWindows(Rect insets) {
int leftPadding = insets.
int rightPadding = insets.
int topPadding = insets.
int bottomPadding = insets.
if (!mActionbarOverlay) {
Log.v(TAG, &setting padding!&);
setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
private Handler mHandler = new Handler();
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void manageLayers(float percentOpen) {
if (Build.VERSION.SDK_INT & 11)
boolean layer = percentOpen & 0.0f && percentOpen & 1.0f;
final int layerType = layer ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE;
if (layerType != getContent().getLayerType()) {
mHandler.post(new Runnable() {
public void run() {
Log.v(TAG, &changing layerType. hardware? & + (layerType == View.LAYER_TYPE_HARDWARE));
getContent().setLayerType(layerType, null);
getMenu().setLayerType(layerType, null);
if (getSecondaryMenu() != null) {
getSecondaryMenu().setLayerType(layerType, null);
看了这个类实现的方法我们在调用时就会知道需要调用那些方法了!
那么接下来,就是告诉大家我们怎么去用这个库。
首先是我们新建一个项目:右键点击---&Properties
这样我们就完成了对SlidingMenu库有了一定了解,下一步我们会详细把一个完成的项目实现过程,给大家介绍一下。这篇到此就结束了,欢迎关注下一篇!
GitHub开源项目SlidingMenu类库
(本文来自互联网,不代表搜站(/)的观点和立场)本站所有内容来自互联网,若本站收录的信息无意侵犯了贵司版权,请给我们来信(),我们会及时处理和回复,谢谢编辑推荐 -
- 最近更新

我要回帖

更多关于 slidingmenu关闭菜单 的文章

 

随机推荐