Android SlidingMenu使用詳解

來源:互聯網
上載者:User

標籤:陰影   ext   undle   ade   匯入   com   添加   XML   u方法   

SlidingMenu是安卓程式中的側滑介面,和系統內建的DrawerLayout的作用是相同的,主要是視覺效果不一樣,手機版QQ使用的側滑介面就是SlidingMenu

 

初步使用方法:實現側滑介面滑出效果

步驟一:下載好SlidingMenu類庫檔案後匯入到項目中

SlidingMenu類庫網盤:

連結:https://pan.baidu.com/s/1df7GKm  密碼:vg50  

將下載好的SlidingMenu-master檔案中的library匯入到當前項目中

匯入後注意修改library的build.gradle中的以下資訊和當前項目的一致

之後可能會報一個錯,顯示的是FloatMath已經到期,手動將FloatMath改成Math就好

 

步驟二:成功匯入類庫後就可以在java代碼中使用SlidingMenu這個類啦,通過java代碼實現側滑介面效果

先上activity_main.xml中的布局:

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:orientation="vertical"    android:background="#ed0e0e"    android:layout_height="match_parent"    tools:context="com.contentprovide.liuliu.slidmingmenu_4.MainActivity">    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>

 

 側滑介面需要引用的自訂的資源布局slid.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">   <TextView       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="我是側滑介面"       /></LinearLayout>

Java代碼實現: 

package com.contentprovide.liuliu.slidmingmenu_4;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;public class MainActivity extends AppCompatActivity {    SlidingMenu slidingMenu;    Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        slidingMenu = new SlidingMenu(this);//        設定呈現模式        slidingMenu.setMode(SlidingMenu.LEFT);//呈現模式為左邊//        設定側滑介面位移出的尺寸        slidingMenu.setBehindOffset(200);//        設定全屏都可以觸摸        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//        添加到當前Activity當中        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//        給側滑介面引用一個布局資源作為所展示的介面        slidingMenu.setMenu(R.layout.slid);//        給主介面的按鈕添加事件,點擊滑出菜單        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                slidingMenu.toggle(true);            }        });    }}

 實現效果:

 

 

 

SlidingMenu常用屬性

 

//設定側滑菜單的位置,可選值LEFT , RIGHT , LEFT_RIGHT (兩邊都有菜單時設定)

 

menu.setMode(SlidingMenu.LEFT_RIGHT);

 

// 設定觸控螢幕幕的模式,可選只MARGIN , CONTENT 
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

 

//根據dimension資源檔的ID來設定陰影的寬度
menu.setShadowWidthRes(R.dimen.shadow_width);

 

//根據資源檔ID來設定滑動菜單的陰影製作效果
menu.setShadowDrawable(R.drawable.shadow);

 

// 這兩個都是設定滑動菜單視圖的寬度,二選一
//設定SlidingMenu離螢幕的位移量
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//設定寬度
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);

 

 

 

 

 

 

進階使用方法:給在SlidingMenu中的布局中的各種空間添加事件,結合使用Fragment

滑出側滑介面後我們需要在側滑介面中進行下一步操作,這樣側滑介面中的布局裡的事件就要單獨寫了

通過serMenu(a)方法給側滑介面引用了布局介面a.xml,在這個a.xml布局介面中只有一個FrameLayout,側滑介面中的所有代碼是寫在Fragment中的,然後通過使用FragmentManager將Fragment替換掉此Fragment,這下就相當於setMenu方法引用的是Fragment了

實現效果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Android SlidingMenu使用詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.