Android开源库——SlidingMenu

SlidingMenu

概述

一款侧滑菜单的开源类库~

SlidingMenu is an Open Source Android library that allows developers to easily create applications with sliding menus like those made popular in the Google+, YouTube, and Facebook apps. Feel free to use it all you want in your Android apps provided that you cite this project and include the license in your app.

项目地址:https://github.com/jfeinstein10/SlidingMenu

在Android Studio中集成SlidingMenu

  1. 创建Android Studio项目
  2. 把下载的SlidingMenu集成到项目中File->New->Import Module->选择SlidingMenu下的library
  3. 根据构造错误修改配置文件

然后会有若干错误,逐个按照提示去解决。

 


入门使用

//configure the SlidingMenu                                                                                    
menu=new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
//设置触摸屏幕测试
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//设置渐入渐出效果的值
menu.setFadeDegree(0.55f);
menu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
//为侧滑菜单设置布局文件
menu.setMenu(R.layout.left_menu);

小案例

准备一个布局文件作为菜单的布局文件

menu_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置"
        android:id="@+id/set" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开"
        android:id="@+id/open" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="退出"
        android:id="@+id/exit" />
</LinearLayout>

MainActivity

package com.example.slidingmenu;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Slide;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建侧滑菜单
        SlidingMenu menu=new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        //设置触摸屏幕测试
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        //设置渐入渐出效果的值
        menu.setFadeDegree(0.55f);
        menu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局文件
        menu.setMenu(R.layout.menu_layout);

    }
}

效果如?

插一个错误

运行时会报个缺v4的错误,引入即可,这里引入最新版的了...


SlidingMenu的常用属性

 

 

 

 

  • 设置侧滑菜单的位置:可选值LEFT , RIGHT , LEFT-RIGHT (两边都有菜单时设置)
  • menu.setMode(SlidingMenu.LEFT RIGHT);
  • 设置触摸屏幕的模式:可选RMARGIN , CONTENT
  • menu.setTouchModeAbove(SlidingMenu. TOUCHMODEMARGIN);
  • 根据dimension资源文件的ID来设置阴影的宽度
  • menu.setShadowwidthRes(R.dimen.shadow width);
  • 根据资源文件ID来设置滑动菜单的阴影效果
  •  menu.setShadowDrawable(R.drawable.shadow);
  • 下面这两个都是设置滑动菜单视图的宽度,二选一
  • menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//设置SlidingMenu离屏幕的偏移量
  • menu.setBehindWidth()//设置宽度

 

  • 设置渐入渐出效果的值
  • menu.setFadeDegree(0.35f);

 

  • 设置SlidingMenu与下方视图的移动的速度比,当为1时同时移动,取值0-1
  • menu.setBehindScrollScale(1.0f);

 

  • 设置二级菜单的阴影效果
  • menu.setSecondaryShadowDrawable(R.drawable.shadow);

 

  • 设置右边(二级)侧滑菜单
  • menu.setSecondaryMenu(R.layout.right_menu_frame);

 

  • 为侧滑菜单设置布局
  • menu.setMenu(R.layout.leftmenu);

 

  • 把滑动菜单添加进所有的Activity中,可选值SLIDING_CONTENT, SLIDING WINDOW
  • menu.attachToActivity(this, SlidingMenu.SLIDING CONTENT)

设置屏幕偏移量测试

需要准备dimen.xml(values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="menu_offset">150dp</dimen>
</resources>

左一为设置了屏幕的偏移量100dp,左二为150dp,左三为200dp,最右为什么都没设置


通过SlidingMenu支持的Activity实现侧滑

SlidingMenu是可以支持通关Activity直接实现侧滑的,主要是以下四种

  1. SlidingActivity
  2. SlidingListActivity
  3. SlidingFragmentActivity
  4. SlidingPreferenceActivity

SlidingActivity

我们新建Activity,以第一种为例,区别在注释中已经给出,效果几乎一样

package com.example.slidingmenu;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;

public class Main2Activity extends SlidingActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);
        //在这里进行
        setBehindContentView(R.layout.menu_layout);
        //SlidingMenu不需要再New了
        //SlidingMenu menu=new SlidingMenu(this);
        SlidingMenu menu = getSlidingMenu();
        menu.setMode(SlidingMenu.LEFT);
        //设置触摸屏幕测试
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        //设置渐入渐出效果的值
        menu.setFadeDegree(0.55f);

        //attachToActivity也不需要了
        //menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

        //不需要再为侧滑菜单设置布局文件,而是在开头...
        //menu.setMenu(R.layout.menu_layout);

        //设置相对屏幕的偏移量
        menu.setBehindOffsetRes(R.dimen.menu_offset);

        //颜色
        menu.setBackgroundColor(Color.LTGRAY);
        //速度比
        //menu.setBehindScrollScale(1.0f);

    }
}

 SlidingListActivity

如果是SlidingListActivity的话,主要是需要修改布局成为一个ListView,并提供ID

或者你自己建一个ListView的xml文件,修改setContentView(R.layout.yourxml)也可以~

Activity部分代码需要注意的和上面完全一样

如下

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main3Activity"
    android:id="@android:id/list">

</ListView>

 


其它

剩下两种顾名思义,就是支持Fragment和Preference....


在布局文件中使用SlidingMenu

    <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
        xmlns:sliding="http://schemas.android.com/apk/res-auto"
        android:id="@+id/slidingmenulayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffffff"
        sliding:behindOffset="100dp"
        sliding:behindScrollScale="1"
        sliding:fadeDegree="0.3"
        sliding:fadeEnabled="true"
        sliding:touchModeAbove="fullscreen"
        sliding:viewAbove="@layout/menu_layout"

        />

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云无心鸟知还

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值