Android Sliding Menu--侧滑菜单

徐瑞
2023-12-01
1.下载SlidingMenu-master.zip包;

2.将SlidingMenu-master.zip解压;

3.将文件夹中的library导入eclipse或者AndroidStudio;

4.若在AndroidStudio中导入,则需要更改配置.gradle文件的配置信息.

5.源码如下:

---menu.xml源码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">


    <FrameLayout
        android:background="#2c90c9"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button 
            android:id="@+id/btn_quit"
            android:text="退出"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </FrameLayout>

</LinearLayout>





---activity_main.xml源码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.slidingmenu.MainActivity" >

    <Button 
        android:id="@+id/btn_show"
        android:onClick="show"
        android:text="显示"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>





---activity源码



public class MainActivity extends Activity {
  SlidingMenu menu;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  menu = new SlidingMenu(this);
  //设置进入的方向
  menu.setMode(SlidingMenu.LEFT);
  // 设置触摸屏幕的模式
  menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
  //设置阴影宽度
  menu.setShadowWidth(15);
  //设置阴影宽度--资源文件夹中的数据
//  menu.setShadowWidthRes(R.dimen.shadow_width);
  //设置阴影图片
  menu.setShadowDrawable(null);
  // 设置滑动菜单空白视图的宽度
  menu.setBehindOffset(60);
  //设置滑动菜单空白视图的宽度--资源文件夹中的数据
//  menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
  //设置渐入渐出效果的值
  menu.setFadeDegree(0.35f);
  //关键代码,无此代码不能拉出侧滑菜单
  menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

  View view = View.inflate(this, R.layout.left_menu, null);
  //获得布局文件中的按钮
  Button btn_quit = (Button)view.findViewById(R.id.btn_quit);
  btn_quit.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    menu.toggle();//退出侧滑菜单
   }
  });
  //为侧滑菜单设置布局
  menu.setMenu(view);
 }

 public void show(View v) {  
  menu.showMenu();
 }
}

点击链接查看效果

 类似资料: