- 在Activity中通过SlidingMenu的构造方法,直接设置侧滑菜单
public class Main2Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); // 实例化SlidingMenu SlidingMenu slidingMenu = new SlidingMenu(this); //设置滑动模式 slidingMenu.setMode(SlidingMenu.LEFT); // 设置主屏幕占据大小 slidingMenu.setBehindOffset(500); //设置滑动的屏幕范围 slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); // 设置菜单布局 slidingMenu.setMenu(R.layout.activity_leftmenu); // 关联Activity slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT,false); }
2. 继承SlidingActivity实现侧滑菜单
public class Main3Activity extends SlidingActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置主页面的布局 setContentView(R.layout.activity_main3); // 设置菜单布局 setBehindContentView(R.layout.activity_leftmenu); final SlidingMenu slidingMenu = getSlidingMenu(); // 设置主页面偏移位置 slidingMenu.setBehindOffset(500); // 设置滑动菜单位置 slidingMenu.setMode(SlidingMenu.LEFT); // 设置滑动方向 slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); findViewById(R.id.btn_left).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView textView = (TextView) findViewById(R.id.tv_left); textView.setText("我是侧边栏的文本"); TextView textView1 = (TextView)findViewById(R.id.tv_content); textView1.setText("我的内容被侧边栏的按钮改变了"); } }); findViewById(R.id.btn_content).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView textView = (TextView) findViewById(R.id.tv_left); textView.setText("我是侧边栏的文本,我的内容被主页面的按钮改变了"); TextView textView1 = (TextView)findViewById(R.id.tv_content); textView1.setText("我的内容被主页的按钮改变了"); } }); } }
3. 通过继承SlidingFragmentActivity实现滑动菜单
public class MainActivity extends SlidingFragmentActivity { public static final String MAIN_CONTENT_TAG = "main_content_tag"; public static final String LEFTMENU_TAG = "leftmenu_tag"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置主页面 setContentView(R.layout.activity_main); // 设置侧边栏 setBehindContentView(R.layout.activity_leftmenu); // 设置滑动的屏幕范围 SlidingMenu slidingMenu = getSlidingMenu(); slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); // 设置主屏幕占据大小 slidingMenu.setBehindOffset(DensityUtil.dip2px(MainActivity.this,500)); // 设置模式 slidingMenu.setMode(SlidingMenu.LEFT); //设置Fragment FragmentManager fm = getSupportFragmentManager(); // 开启事务 FragmentTransaction ft = fm.beginTransaction(); // 替换 ft.replace(R.id.fl_main_content,new ContentFragment(),MAIN_CONTENT_TAG); ft.replace(R.id.fl_leftmenu,new LeftMenuFragment(),LEFTMENU_TAG); // 提交 ft.commit(); } }