Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開源菜單庫 SlidingMenu 深入剖析

編輯:關於 Android 程式設計

SlidingMenu 簡介

SlidingMenu 是一種較新的側滑式介面設計,在主界面左滑或右滑即可唤出設定或功能頁面,操作直覺便利。目前已有大量應用採用,如 Evernote、Google+、Foursquare,国内的豌豆夾、人人、360 手機助手等也都使用這套方案。

注意:SlidingMenu 依賴另一個開源專案 ActionBarSherlock,需先將 ActionBarSherlock 加入為 SlidingMenu 的庫工程,否則會報資源找不到的錯誤,然後再把 SlidingMenu 加入自己的專案。

常見整合錯誤

Jar mismatch! Fix your dependencies:引用工程與自身工程相依的 jar 包版本不一致所致,確保兩者 jar 版本一致即可。

若使用到 ActionBarSherlock 的 API 卻報找不到 getSupportActionBar 等方法,是因為使用它的 Activity 需繼承自 SherlockActivity。可修改 SlidingMenu 庫中的 SlidingFragmentActivity 改繼承 SherlockFragmentActivity,重新編譯後匯入。

常用屬性

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);// 菜單劃出時主頁面的剩余寬度 menu.setBehindWidth(400);// 設置菜單寬度 menu.setFadeDegree(0.35f);// 滑動時的漸變程度 menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);// 附加在 Activity 上 menu.setMenu(R.layout.menu_layout);// 設置 menu 布局 menu.toggle();// 自動開關 menu.showMenu();// 顯示菜單 menu.showContent();// 顯示內容 menu.setOnOpenListener(onOpenListener);// 監聽打開

若要同時支持左右兩側劃出菜單,只需設為 LEFT_RIGHT 並設定右側菜單布局:

menu.setMode(SlidingMenu.LEFT_RIGHT); menu.setSecondaryShadowDrawable(R.drawable.shadowright);// 右側陰影

用 Fragment 實作 SlidingMenu

1. Activity 繼承 SlidingFragmentActivity;
2. setContentView(R.layout.content_frame)(全屏 FrameLayout);
3. setBehindContentView(R.layout.menu_frame);
4. 設置左側菜單 Fragment。

setBehindContentView(R.layout.menu_frame); FragmentTransaction t = this.getSupportFragmentManager().beginTransaction(); leftMenuFragment = new MenuFragment(); t.replace(R.id.menu_frame, leftMenuFragment); t.commit();

MenuFragment 本質就是一個顯示 ListView 的 Fragment,點擊項目時通知 Activity 切換不同 Fragment。這裡建立 Fragment1~Fragment5 展示效果。

設置主頁面顯示的 Fragment:

if (savedInstanceState == null) { contentFragment = new Fragment1(); } else { contentFragment = getSupportFragmentManager().getFragment(savedInstanceState, contentFragment); } getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, contentFragment) .commit();

並在 onSaveInstanceState 中保存當前 Fragment,再設置 SlidingMenu 屬性(setMode、setShadowDrawable、setBehindOffsetRes、setTouchModeAbove 等)。

支持右側劃出菜單:

sm.setSecondaryMenu(R.layout.menu_frame2); sm.setSecondaryShadowDrawable(R.drawable.shadowright); getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame2, new SampleListFragment()).commit();

讓 ActionBar 可點擊:getSupportActionBar().setHomeButtonEnabled(true) 與 setDisplayHomeAsUpEnabled(true)。

用普通 Activity 實作

SlidingMenu menu = new SlidingMenu(this);// 直接 new menu.setMode(SlidingMenu.LEFT); menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); menu.setShadowDrawable(R.drawable.shadow); menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); menu.setBehindWidth(400); menu.setFadeDegree(0.35f); menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);// 必須調用 menu.setMenu(R.layout.menu_layout_left); menu.setBehindCanvasTransformer(mTransformer);

由於 SlidingMenu 已被包含進 Activity,直接 findViewById 拿到 view 後即可處理點擊事件。

更換動畫

SlidingMenu 支持滑動時定義縮放、旋轉、拉伸等動畫,透過 CanvasTransformer 實作:

mTransformer = new CanvasTransformer() { @Override public void transformCanvas(Canvas canvas, float percentOpen) { float scale = (float) (percentOpen*0.25 + 0.75); canvas.scale(scale, scale, canvas.getWidth()/2, canvas.getHeight()/2); } };

拉伸動畫則可寫成 canvas.scale(percentOpen, 1, 0, 0),將 transformer 設定給 SlidingMenu 即可生效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved