第三方側滑菜單SlidingMenu在android studio中的使用


南塵:每天進步一點點!

 

前面講了官方的側滑菜單DrawerLayout的使用,其實早在官方沒有推出這個之前,就有很多第三方的jar包如SlidingMenu等,感謝開源的力量。

SlidingMenu是一個開源的側滑菜單(https://github.com/jfeinstein10/SlidingMenu)。 為大家的安卓程序提供側滑菜單,這個功能也非常有用。


配置:
本人親測使用第三方jar包在Eclipse下面可以正常使用,而在andorid studio下面直接導入jar包加入app中會報一個什么index越界的異常,所以通過多方資料了解到應該這樣配置。

1)先從github上下載這個zip壓縮包並解壓出來。里面的library文件夾就是我們需要的東西。

 

 

2)導入依賴庫

3)切換到project視圖,分別打開app下面的 build.gradle跟library下的build.gradle。將library 下的gradle 版本修改與 本機 版本的相符 , 本機版本在根目錄下的build.gradle 查看 注意分清楚!

 

 

 

4)別忘了你還需要app點擊鼠標右鍵點開module setting 添加依賴,其實這些東西你都可以在配置文件中更改。不過我更喜歡用鼠標來點。

 

 

 

 

好了,到這里,我們的SlidingMenu的library就成功地導入我們所寫的app中了。下面就可以成功調用。

上運行圖:

 

上代碼:

package com.example.nanchen.slidingmenudemo;

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

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);

        // configure the SlidingMenu
        SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        // 設置觸摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.color.colorAccent);

        // 設置滑動菜單視圖的寬度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 設置漸入漸出效果的值
        menu.setFadeDegree(0.35f);
        /**
         * SLIDING_WINDOW will include the Title/ActionBar in the content
         * section of the SlidingMenu, while SLIDING_CONTENT does not.
         */
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //為側滑菜單設置布局
        menu.setMenu(R.layout.left_menu);
    }
}

  

activity_main

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nanchen.slidingmenudemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
</RelativeLayout>

  left_menu.xml

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


    <ListView
        android:background="#2c90c9"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

  

<dimen name="slidingmenu_offset">60dp</dimen>
<dimen name="shadow_width">15dp</dimen>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM