相信大家都使用过这些功能,也相信都见过这样的布局Slidingmenu,开始在看到这样的布局的时候,相信大多数人的做法都是通过动画实现的。这样的设计是比较合情合理的。
相信大家只要是开发android的肯定也了解https://github.com这个平台,而我要说的这个Slidingmenu就是出自该平台的Jfeinstein10所创。而我只是将他的杰作封装了一下,打了一个jar包,将该布局作为一个控件的形式体现出来。在google上搜索Slidingmenu就可以发现该控件的信息,但使用的方法却较为繁琐,需要:
- 先导入ABS项目工程
- 再导入library工程,而且library工程需要添加ABS的依赖。
- 随后才可以新建自己工程,而且自己的工程还需要依赖library工程。
我就只把他的属性配置给拷贝过来。
viewAbove
- a reference to the layout that you want to use as the above view of the SlidingMenuviewBehind
- a reference to the layout that you want to use as the behind view of the SlidingMenutouchModeAbove
- an enum that designates what part of the screen is touchable when the above view is showing. Margin means only the left margin. Fullscreen means the entire screen. Default is margin.behindOffset
- a dimension representing the number of pixels that you want the above view to show when thebehind view is showing. Default is 0.behindWidth
- a dimension representing the width of the behind view. Default is the width of the screen(equivalent to behindOffset = 0).behindScrollScale
- a float representing the relationship between the above view scrolling and the behindbehind view scrolling. If set to 0.5f, the behind view will scroll 1px for every 2px that the above view scrolls.If set to 1.0f, the behind view will scroll 1px for every 1px that the above view scrolls. And if set to 0.0f, thebehind view will never scroll; it will be static. This one is fun to play around with. Default is 0.25f.shadowDrawable
- a reference to a drawable to be used as a drop shadow from the above view onto the below view.Default is no shadow for now.shadowWidth
- a dimension representing the width of the shadow drawable. Default is 0.fadeEnabled
- a boolean representing whether or not the behind view should fade when the SlidingMenu is closingand "un-fade" when openingfadeDegree
- a float representing the "amount" of fade.1.0f
would mean fade all the way to black when theSlidingMenu is closed.0.0f
would mean do not fade at all.selectorEnabled
- a boolean representing whether or not a selector should be drawn on the left side of the aboveview showing a selected view on the behind view.selectorDrawable
- a reference to a drawable to be used as the selectorNOTE : in order to have the selector drawn, you must call SlidingMenu.setSelectedView(View v) with the selected view.Note that this will most likely not work with items in a ListView because of the way that Android recycles item views.
接下来谈一下我封装的library。
需要做的只需要将包导入项目即可。
看一下如何用代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SlidingMenu menu = new SlidingMenu(this);//初始化组件
Button button = new Button(this);//添加一个不同用于控制menu
button.setText("mybutton");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menu.toggle();//滑动的方法
}
});
RelativeLayout view = new RelativeLayout(this);
view.addView(button);
setContentView(view);//主视图
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.setBehindWidth(200);//菜单宽带
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.activity_main);//添加菜单
}
就这些代码即可实现,而且效果可以通知自己的配置随便设置。需要注意的是下面的参数是通过配置文件控制的 menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);//菜单滑动时阴影部分
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
使用时请注意:menu菜单的视图通过menu.setMenu控制。而左侧的视图就是直接通过setContentView控制的。
好了分享一下library:
http://download.csdn.net/detail/woaixinxin123/5710131 [无需积分]
分享一下demo:
http://download.csdn.net/detail/woaixinxin123/5710155 [积分3]
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
最近发现只单纯的左右滑动已经满足不了人们的欲望了。我研究了下源码发现在我封装的这个jar里也可以使用左右一块换到的效果。
左右滑动代码展示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SlidingMenu menu = new SlidingMenu(this);
Button button = new Button(this);
button.setText("left");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menu.showMenu();
}
});
Button button2 = new Button(this);
button2.setText("right");
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menu.showSecondaryMenu();
}
});
RelativeLayout view = new RelativeLayout(this);
LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
llp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
view.addView(button,llp);
LayoutParams rlp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
view.addView(button2,rlp);
setContentView(view);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setBehindWidth(200);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.activity_main);
menu.setMode(SlidingMenu.LEFT_RIGHT);
menu.setSecondaryMenu(R.layout.activity_main);
menu.setSecondaryShadowDrawable(R.drawable.shadow);
menu.setShadowDrawable(R.drawable.shadow);
}
为了更方便的使用该控件我再次写了一下demo,分为两个activity一个是左右一块滑动的,一个是单方向滑动的。看不懂的可以QQ联系我。
http://download.csdn.net/detail/woaixinxin123/5883503 【3积分】
你没有理由不喜欢,除非你不需要。本人android,web开发 QQ群255825960
null
2013/7/6