SlidingMenu菜单设置左右菜单不同宽度

修改SlidingMenu的源码,让其可以分别设置左右菜单的宽度,具体修改如下:

1、在源码SlidingMenu中加入以下代码:

  1. // 设置右侧边栏菜单展开时距离左边界的偏移量

  2.         public void setRightMenuOffset(int offset) {

  3.                 mViewBehind.setRightWidthOffset(offset);

  4.         }


  5.         // 设置右侧边栏菜单展开时距离左边界的偏移量

  6.         public void setRightMenuOffsetRes(int resId) {

  7.                 int i = (int) getContext().getResources().getDimension(resId);

  8.                 setRightMenuOffset(i);

  9.         }


  10.         // 设置右侧边栏的宽度

  11.         @SuppressWarnings("deprecation")

  12.         public void setRightBehindWidth(int i) {

  13.                 int width;

  14.                 Display display = ((WindowManager) getContext().getSystemService(

  15.                                 Context.WINDOW_SERVICE)).getDefaultDisplay();

  16.                 try {

  17.                         Class<?> cls = Display.class;

  18.                         Class<?>[] parameterTypes = { Point.class };

  19.                         Point parameter = new Point();

  20.                         Method method = cls.getMethod("getSize", parameterTypes);

  21.                         method.invoke(display, parameter);

  22.                         width = parameter.x;

  23.                 } catch (Exception e) {

  24.                         width = display.getWidth();

  25.                 }

  26.                 setRightMenuOffset(width - i);

  27.         }


  28.         // 设置右侧边栏的宽度

  29.         public void setRightBehindWidthRes(int res) {

  30.                 int i = (int) getContext().getResources().getDimension(res);

  31.                 setRightBehindWidth(i);

  32.         }



2、修改CustomViewBehind类中的scrollBehindTo方法

  1. public void scrollBehindTo(View content, int x, int y) {

  2.                 int vis = View.VISIBLE;

  3.                 if (mMode == SlidingMenu.LEFT) {

  4.                         if (x >= content.getLeft())

  5.                                 vis = View.INVISIBLE;

  6.                         scrollTo((int) ((x + getBehindWidth()) * mScrollScale), y);

  7.                 } else if (mMode == SlidingMenu.RIGHT) {

  8.                         if (x <= content.getLeft())

  9.                                 vis = View.INVISIBLE;

  10.                         scrollTo(

  11.                                         (int) (getRightBehindWidth() - getWidth() + (x - getRightBehindWidth())

  12.                                                         * mScrollScale), y);

  13.                 } else if (mMode == SlidingMenu.LEFT_RIGHT) {

  14.                         mContent.setVisibility(x >= content.getLeft() ? View.INVISIBLE

  15.                                         : View.VISIBLE);

  16.                         mSecondaryContent

  17.                                         .setVisibility(x <= content.getLeft() ? View.INVISIBLE

  18.                                                         : View.VISIBLE);

  19.                         vis = x == 0 ? View.INVISIBLE : View.VISIBLE;

  20.                         if (x <= content.getLeft()) {

  21.                                 scrollTo((int) ((x + getBehindWidth()) * mScrollScale), y);

  22.                         } else {

  23.                                 scrollTo(

  24.                                                 (int) (getRightBehindWidth() - getWidth() + (x - getRightBehindWidth())

  25.                                                                 * mScrollScale), y);

  26.                         }

  27.                 }

  28.                 if (vis == View.INVISIBLE)

  29.                         Log.v(TAG, "behind INVISIBLE");

  30.                 setVisibility(vis);

  31.         }


  1. 3、修改CustomViewBehind类中的getMenuLeft方法

  2. public int getMenuLeft(View content, int page) {

  3.                 if (mMode == SlidingMenu.LEFT) {

  4.                         switch (page) {

  5.                         case 0:

  6.                                 return content.getLeft() - getBehindWidth();

  7.                         case 2:

  8.                                 return content.getLeft();

  9.                         }

  10.                 } else if (mMode == SlidingMenu.RIGHT) {

  11.                         switch (page) {

  12.                         case 0:

  13.                                 return content.getLeft();

  14.                         case 2:

  15.                                 return content.getLeft() + getRightBehindWidth();

  16.                         }

  17.                 } else if (mMode == SlidingMenu.LEFT_RIGHT) {

  18.                         switch (page) {

  19.                         case 0:

  20.                                 return content.getLeft() - getBehindWidth();

  21.                         case 2:

  22.                                 return content.getLeft() + getRightBehindWidth();

  23.                         }

  24.                 }

  25.                 return content.getLeft();

  26.         }


    1. 4、修改CustomViewBehind类中的getAbsRightBound方法

    1. public int getAbsRightBound(View content) {

    2.                 if (mMode == SlidingMenu.LEFT) {

    3.                         return content.getLeft();

    4.                 } else if (mMode == SlidingMenu.RIGHT

    5.                                 || mMode == SlidingMenu.LEFT_RIGHT) {

    6.                         return content.getLeft() + getRightBehindWidth();

    7.                 }

    8.                 return 0;

    9.         }



    1. 引用的时候左边菜单设置方法不变,右边菜单这么设置:

    1. // 设置右边菜单

    2.                 sm.setSecondaryMenu(R.layout.menu_right_layout);

    3.                 sm.setSecondaryShadowDrawable(R.drawable.shadowright); // 设置右菜单阴影图片

    4.                 sm.setRightMenuOffsetRes(R.dimen.right_menu_offset);//设置右菜单宽度

     项目源码下载地址:http://download.csdn.net/detail/u010820846/8336917


    你可能感兴趣的:(android,宽度,SlidingMenu,左右菜单)