Android--SlidingMenu第三方库实现侧滑效果

来源:互联网 发布:linux删除指令 编辑:程序博客网 时间:2024/05/01 08:27

SlidingMenu简单操作步骤:
1.导入依赖类库SlidingMenu;

2.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"                android:background="#ff418cff"                tools:context="com.longshun.animationdemo.customview.YouTubeActivity">    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="btnToggle"            android:text="切换菜单"            /></RelativeLayout>

菜单布局文件 R.layout.menu_first

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent"        >    <TextView            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:textSize="38dp"            android:textColor="#000"            android:text="侧滑菜单测试案例!"            /></LinearLayout>

菜单布局文件 R.layout.menu_second

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:background="#0f0"        ></LinearLayout>

程序入口布局文件 activity_swiperefresh_demo.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"                android:background="#ff418cff"                tools:context="com.longshun.animationdemo.customview.YouTubeActivity">    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="btnToggle"            android:text="切换菜单"            /></RelativeLayout>

3.java代码:

package com.longshun.animationdemo.customview;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;public class SwipeRefreshDemoActivity extends AppCompatActivity implements SlidingMenu.OnOpenListener, SlidingMenu.OnCloseListener {    private SlidingMenu menu;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_you_tube);        //设置SlidingMenu        //1.创建SlidingMenu对象实例        //参数一:哪一个Activity显示侧滑菜单        //参数二:滑动的样式,包括:侧滑Window;保留标题栏侧滑。        //menu = new SlidingMenu(this,SlidingMenu.SLIDING_WINDOW);        menu = new SlidingMenu(this,SlidingMenu.SLIDING_CONTENT);        //2.设置菜单在左侧还是右侧        menu.setMode(SlidingMenu.LEFT_RIGHT);        //3.设置主要菜单(第一个菜单)        menu.setMenu(R.layout.menu_first);//左侧        //3.1次要菜单        menu.setSecondaryMenu(R.layout.menu_second);//右侧        //4.菜单滑出,Activity右移,设置右移后的Activity能被看到的宽度        //menu.setBehindOffset(200);        //4.1.设置滑出菜单的宽度        menu.setBehindWidth(200);        //5.设置在内容边界点滑动时滑出菜单,        // 还是在整个Activity屏幕滑动的时候滑出菜单        //6.设置滑动菜单时隐藏菜单;菜单上的有效滑动区域        menu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);        //7.设置菜单缩放比例,拉出来的效果;        // 0正常显示隐藏在后面的内容,        //1后面页面右边拼接在主界面左边显示出内容        menu.setBehindScrollScale(1);        //8.设置菜单监听事件        menu.setOnOpenListener(this);//菜单滑出        menu.setOnCloseListener(this);//菜单关闭    }    //判断是否显示菜单    //隐藏菜单    //Toggle切换菜单显示    @Override    public void onBackPressed() {        if (menu.isMenuShowing()){            //隐藏菜单的开关            menu.toggle();        }else {            super.onBackPressed();        }    }    @Override    public void onOpen() {        setTitle("菜单");    }    @Override    public void onClose() {        setTitle(R.string.app_name);    }    public void btnToggle(View view) {        menu.toggle();    }}
0 0
原创粉丝点击