Android 側滑菜單實現

來源:互聯網
上載者:User

標籤:slidingmenu

Android中很多產品都採用側滑菜單的展現形式,採用這種展現形式

1:能把更多的展現內容都存放在菜單中

2:設計上也能體現出視覺效果

先看下360手機小幫手,


現在這種互動方式越來越流行了,雖然這種互動方式可以通過ViewGroup的方式來實現,但是用三方開源庫更簡單。

SlidingMenu:SlidingMenu的是一種比較新的設定介面或配置介面效果,在主介面左滑或者右滑出現設定介面,能方便的進行各種操作.目前有大量的應用都在使用這一效果

現在建立一個Android項目SlidingMenuDemo,

activity_main:

<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"
     >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="內容" 
        android:textSize="24sp"
        android:textColor="#000"
        android:gravity="center"
        />
</RelativeLayout>


左邊的菜單layout,activity_menu:

<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="fill_parent"
        android:layout_height="40dp"
        android:gravity="center" 
android:text="體育"
android:textSize="16sp"
android:textColor="#000"        
        />
 <TextView
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center" 
android:text="娛樂"
android:textSize="16sp"
android:textColor="#000"        
        />
  <TextView
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center" 
android:text="財經"
android:textSize="16sp"
android:textColor="#000"        
        />
   <TextView
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center" 
android:text="科技"
android:textSize="16sp"
android:textColor="#000"        
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center" 
android:text="杭州"
android:textSize="16sp"
android:textColor="#000"        
        />
</LinearLayout>


現在看MainActivity.java:

package com.example.slidingmenudemo;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;


public class MainActivity extends SlidingFragmentActivity {
private SlidingMenu sm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 1 設定滑動菜單旁邊的頁面
setBehindContentView(R.layout.activity_menu);
setContentView(R.layout.activity_main);

//2 擷取滑動菜單
sm = getSlidingMenu();

sm.setMode(SlidingMenu.LEFT);
// 4 設定滑動菜單出來之後,內容頁剩餘的寬度
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

// 6 設定陰影
sm.setShadowDrawable(R.drawable.shadow);
sm.setShadowWidthRes(R.dimen.shadow_width);
}
}


注意Activity一定要繼承SlidingFragmentActivity



現在看看SlidingMenu一些屬性:

menu.setMode(SlidingMenu.LEFT);//設定左滑菜單

menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//設定滑動的螢幕範圍,該設定為全屏地區都可以滑動

menu.setShadowDrawable(R.drawable.shadow);//設定陰影圖片

menu.setShadowWidthRes(R.dimen.shadow_width);//設定陰影圖片的寬度

menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu划出時首頁面顯示的剩餘寬度

menu.setBehindWidth(400);//設定SlidingMenu菜單的寬度

menu.setFadeDegree(0.35f);//SlidingMenu滑動時的漸層程度

menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//使SlidingMenu附加在Activity上

menu.setMenu(R.layout.menu_layout);//設定menu的布局檔案

menu.toggle();//動態判斷自動關閉或開啟SlidingMenu

menu.showMenu();//顯示SlidingMenu

menu.showContent();//顯示內容


左右都可以划出SlidingMenu菜單只需要設定
menu.setMode(SlidingMenu.LEFT_RIGHT);屬性,然後設定右側菜單的布局檔案

menu.setSecondaryShadowDrawable(R.drawable.shadowright);//右側菜單的陰影圖片


設定SlidingMenu屬性
sm = getSlidingMenu();
//如果只顯示左側菜單就是用LEFT,右側就RIGHT,左右都支援就LEFT_RIGHT
sm.setMode(SlidingMenu.LEFT_RIGHT);//設定菜單滑動模式,菜單是出現在左側還是右側,還是左右兩側都有
sm.setShadowDrawable(R.drawable.shadow);//設定陰影的圖片資源
sm.setShadowWidthRes(R.dimen.shadow_width);//設定陰影圖片的寬度
//sm.setBehindWidth(200);//設定菜單的寬
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu划出時首頁面顯示的剩餘寬度
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//設定滑動的地區

Android 側滑菜單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.