一、控件功能:
左、右、左右滑动的三方开源控件,侧滑菜单或配置其他的内容。
方法可以自由调用。
二、控件的获取和导入:
1.下载:https://github.com/jfeinstein10/SlidingMenu
2.将library导入eclipse并新建工程,将library导入新建的工程。
三、控件的实现思路:
代码实现
例如:
public class MainActivity extends Activity {
private SlidingMenu mSlidingMenu ;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState );
setContentView (R .layout .activity_main );
mSlidingMenu = new SlidingMenu (this);
mSlidingMenu.setMode (SlidingMenu .LEFT_RIGHT );// 设置滑动的方向 向左 、向右或向左向右
mSlidingMenu.attachToActivity (this, SlidingMenu.SLIDING_CONTENT );// 该方法为主键设置滑动的是内容还是整个Windows
mSlidingMenu.setTouchModeAbove (SlidingMenu .TOUCHMODE_FULLSCREEN );// 设置从哪里可以触发滑动效果:全屏、边界、不可以
mSlidingMenu.setTouchModeBehind (SlidingMenu .TOUCHMODE_FULLSCREEN );
mSlidingMenu.setShadowWidth (20 );// 设置阴影的宽度
// mSlidingMenu.setShadowDrawable(R.drawable.shadow);// 设置阴影的图片
mSlidingMenu.setBehindOffsetRes (R .dimen .menu_margin );
mSlidingMenu.setMenu (R .layout .left_menu );
mSlidingMenu.setSecondaryMenu (R .layout .right_menu );// 设置菜单内容
}
}
xml实现:
<com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
xmlns:sliding="http://schemas.android.com/apk/res-auto"
android:id="@+id/slidingmenulayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
sliding:viewAbove="@layout/YOUR_ABOVE_VIEW"
sliding:viewBehind="@layout/YOUR_BEHIND_BEHIND"
sliding:touchModeAbove="margin|fullscreen"
sliding:behindOffset="@dimen/YOUR_OFFSET"
sliding:behindWidth="@dimen/YOUR_WIDTH"
sliding:behindScrollScale="@dimen/YOUR_SCALE"
sliding:shadowDrawable="@drawable/YOUR_SHADOW"
sliding:shadowWidth="@dimen/YOUR_SHADOW_WIDTH"
sliding:fadeEnabled="true|false"
sliding:fadeDegree="float"
sliding:selectorEnabled="true|false"
sliding:selectorDrawable="@drawable/YOUR_SELECTOR"/>
```
四、控件重要的方法
1、public void attachToActivity(Activity activity, int slideStyle)
方法说明:slideStyle为为滑动的方式设置类型,有:SLIDING_WINDOW和SLIDING_CONTENT 滑动的是内容还是窗体
2、public void setContent(int res)
方法说明: 给中间显示的页面设置布局
3、public void setMenu(int res)
方法说明:给菜单设置布局文件
4、public void setSecondaryMenu(int res)
方法说明:设置二级菜单
5、public void setMode(int mode)
方法说明:设置模式--三种模式:LEFT 、RIGHT、 LEFT_RIGHT
6、public void showMenu()
方法说明:打开菜单(侧滑打开)
7、public void showSecondaryMenu()
8、public void showContent()
9、public int getBehindOffset()
方法说明:隐藏视图滚动到屏幕的边缘右边
10、 mSlidingMenu.setTouchModeAbove (SlidingMenu .TOUCHMODE_FULLSCREEN );// 设置从哪里可以触发滑动效果:全屏、边界、不可以
mSlidingMenu.setTouchModeBehind (SlidingMenu .TOUCHMODE_FULLSCREEN );
11、 attachToActivity:
代码块:
switch (slideStyle) {
case SLIDING_WINDOW:
mActionbarOverlay = false ;
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);
break;
case SLIDING_CONTENT:
mActionbarOverlay = actionbarOverlay;
// 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 );
break;
代码说明:举一个例子:
SLIDING_WINDOW
获取window同样大小的ViewGroup ,去掉子ViewGroup ,把子布局加到slidingMenu上