最近有个项目需要使用侧边栏,而且希望是左右两侧都能够滑出侧边菜单,在网上查找实现方式时,发现大家用的最多的还是大神jfeinstein10的SlidingMenu库,地址https://github.com/jfeinstein10/SlidingMenu,通过将开源库导入eclipse,并把它作为library引入自己的工程,经过配置就可以实现SlidingMenu的效果,网上有很多例子,大家可以下一个研究研究,初学者可以直接在例子上修改,开发自己的应用,当然这样不够严谨。

  这次开发并没有采用上面的方式,而是将SlidingMenu开源库作为架包,通过引入jar包来开发项目,这样可以将滑动布局以控件的形式体现出来,使用时比较简单。本次开发参考的代码为http://download.csdn.net/download/woaixinxin123/5883503

MainActivity.java

package com.example.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams; import com.slidingmenu.lib.SlidingMenu;  //引用的SlidigMenu包 public class LeftAndRightActivity extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
} private void initView() { 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); DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels;
int height = metric.heightPixels; menu.setMode(SlidingMenu.LEFT_RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setBehindWidth(width*2/3);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.left_menu_frame);
menu.setShadowDrawable(R.drawable.shadow);
getSupportFragmentManager().beginTransaction().replace(R.id.left_menu_frame, new NavigationFragment()).commit();   //左侧菜单界面由NavigationFragment确定
        menu.setSecondaryMenu(R.layout.right_menu_frame);
menu.setSecondaryShadowDrawable(R.drawable.shadow);
//左右两侧滑出菜单样式可以通过自己写的Fragment定制,在此不再赘述
} }
05-02 04:17