android 简单侧滑实现,Android 侧滑菜单的简单实现(SlidingMenu)二

在上一篇博文中已经简单的实现了侧滑菜单,代码也很简单,就几行代码。

这篇文章依然讲侧滑菜单,与前一篇文章不同的是,这篇文章用不同的代码方式来实现侧滑菜单。

在前面的文章中已经用了在Activity中通过SlidingMenu构造方法直接设置侧滑菜单,这里换成通过Activity继承SlidingActivity来实现侧滑。

代码如下:

public class MainActivity extends SlidingActivity

重写onCreate()方法:

@Overridepublic voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setBehindContentView(R.layout.slidingmenu);//设置侧滑布局

SlidingMenu menu = getSlidingMenu();//获取SlidingMenu

menu.setMode(SlidingMenu.LEFT);//设置触摸屏幕的模式

menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置滑动菜单视图的宽度

menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//设置渐入渐出效果的值

menu.setFadeDegree(0.35f);

}

其余的跟上一篇博文的一样。

运行结果如下:

e502377aabfb6a24f1bd22b4356f475f.png

跟上一篇博文的效果完全一样。

另外还有一种方式,就是把SlidingMenu当作一般的控件使用,在XML布局中直接使用即可。

方式如下:

activity_main.xml:

还需要其它的布局文件:

pic.xml:(其实就是放一张图片)

slidingmenu.xml:(侧滑菜单的布局文件)

接下来是在Activity中的代码了:

@Overrideprotected voidonCreate(Bundle savedInstanceState) {//TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

slidingmenu=(SlidingMenu) findViewById(R.id.slidingmenulayout);

slidingmenu.setMode(SlidingMenu.LEFT);//设置侧滑布局

slidingmenu.setMenu(R.layout.slidingmenu);

slidingmenu.setOnClickListener(newOnClickListener() {

@Overridepublic voidonClick(View v) {if(slidingmenu.isMenuShowing())

slidingmenu.toggle();

}

});

}

运行结果如下:

4db85835869e176bce731f3d75c810e4.gif

下面是SlidingMenu的常用属性:

viewAbove- a reference to the layout that you want to use as the above view of the SlidingMenu

viewBehind- a reference to the layout that you want to use as the behind view of the SlidingMenu

touchModeAbove- 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 the behind 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 behind behind 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, the behind 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 closing and "un-fade" when opening

fadeDegree- a float representing the "amount" of fade.1.0fwould mean fade all the way to black when the SlidingMenu is closed.0.0fwould mean do not fade at all.

selectorEnabled- a boolean representing whether or not a selector should be drawn on the left side of the above view showing a selected view on the behind view.

selectorDrawable- a reference to a drawable to be used as the selector NOTE : 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.

还有一些到时候再查吧。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值