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

Android 自訂控制項輕鬆實現側滑菜單效果

編輯:關於 Android 程式設計

側滑菜單在許多應用中都會見到,最近 QQ 5.0 的側滑還玩了點花樣。對於側滑菜單,一般人通常會自訂 ViewGroup,然後隱藏菜單欄,手指滑動時透過 Scroller 或不斷改變 leftMargin 來實現;這多少有點複雜,完成後還要處理滑動衝突等問題。今天則帶給大家一個相對簡單的實現——說「史上最簡單」或有誇張,但的確是我目前遇到過最簡單的一種做法。

1. 原理分析

既然是側滑,無非就是在巴掌大的螢幕裡塞入大約兩巴掌寬的布局,滑動時讓另一個出現。既然如此,為什麼不考慮使用 Android 提供的 HorizontalScrollView 呢?

如果使用 HorizontalScrollView,還需要在 ACTION_DOWN、ACTION_MOVE 裡不斷監聽、判斷並改變控制項位置嗎?不需要!HorizontalScrollView 本身就具備滑動功能。那還需要手動處理各種衝突嗎?也不需要!當然,還是要了解事件分發機制。

2. 效果圖

3. 布局檔

 <com.example.zhy_slidingmenu.SlidingMenu xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="fill_parent" android:scrollbars="none" > <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="horizontal" > <include layout="@layout/layout_menu" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/qq" > </LinearLayout> </LinearLayout> </com.example.zhy_slidingmenu.SlidingMenu> 

最外層是我們的自訂 View,裡面一個水平方向的 LinearLayout,接著一個是菜單布局,一個是主布局。

4. 自訂 SlidingMenu

接下來就是最核心的程式碼:

 package com.example.zhy_slidingmenu; import android.content.Context; import android.util.AttributeSet; import android.util.TypedValue; import android.view.MotionEvent; import android.view.ViewGroup; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import com.zhy.utils.ScreenUtils; public class SlidingMenu extends HorizontalScrollView { private int mScreenWidth; private int mMenuRightPadding = 50; private int mMenuWidth; private int mHalfMenuWidth; private boolean once; public SlidingMenu(Context context, AttributeSet attrs) { super(context, attrs); mScreenWidth = ScreenUtils.getScreenWidth(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (!once) { LinearLayout wrapper = (LinearLayout) getChildAt(0); ViewGroup menu = (ViewGroup) wrapper.getChildAt(0); ViewGroup content = (ViewGroup) wrapper.getChildAt(1); mMenuRightPadding = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding, content .getResources().getDisplayMetrics()); mMenuWidth = mScreenWidth - mMenuRightPadding; mHalfMenuWidth = mMenuWidth / 2; menu.getLayoutParams().width = mMenuWidth; content.getLayoutParams().width = mScreenWidth; } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (changed) { this.scrollTo(mMenuWidth, 0); once = true; } } @Override public boolean onTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_UP: int scrollX = getScrollX(); if (scrollX > mHalfMenuWidth) this.smoothScrollTo(mMenuWidth, 0); else this.smoothScrollTo(0, 0); return true; } return super.onTouchEvent(ev); } } 

完工!上面的效果只用到這麼點程式碼。除了設定寬度等雜項,真正處理滑動的程式碼不過 10 行。

5. 擴展
接著我們來完善程式。首先把菜單布局改成 ListView 以證明沒有衝突;然後加入一個屬性讓使用者設定菜單距右邊的邊距;再對外提供一個方法,讓使用者點擊某按鈕時菜單能慢慢滑出。

1)加入自訂屬性

a. 在 values 資料夾下新增 attr.xml:

 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="rightPadding" format="dimension" /> <declare-styleable name="SlidingMenu"> <attr name="rightPadding" /> </declare-styleable> </resources> 

b. 在布局中宣告命名空間並使用屬性:

 <com.example.zhy_slidingmenu.SlidingMenu xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" xmlns:zhy="https://schemas.android.com/apk/res/com.example.zhy_slidingmenu" android:layout_width="wrap_content" android:layout_height="fill_parent" android:scrollbars="none" zhy:rightPadding="100dp" > 

其中命名空間 xmlns:zhy 為 https://schemas.android.com/apk/res/ 加上你的包名;屬性 zhy:rightPadding 這裡設為 100dp。

c. 在自訂類別中取得屬性:

 public SlidingMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mScreenWidth = ScreenUtils.getScreenWidth(context); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyle, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.SlidingMenu_rightPadding: mMenuRightPadding = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 50f, getResources().getDisplayMetrics())); break; } } a.recycle(); } 

在三參數的構造方法中,透過 TypedArray 取得即可。若有許多自訂屬性,依同樣步驟處理就行。

2)對外提供打開菜單的方法

先定義一個 boolean isOpen 標識菜單狀態,並記得在 ACTION_UP 時改變狀態:

 case MotionEvent.ACTION_UP: int scrollX = getScrollX(); if (scrollX > mHalfMenuWidth) { this.smoothScrollTo(mMenuWidth, 0); isOpen = false; } else { this.smoothScrollTo(0, 0); isOpen = true; } return true; } 

接著加入方法:

 public void openMenu() { if (isOpen) return; this.smoothScrollTo(0, 0); isOpen = true; } public void closeMenu() { if (isOpen) { this.smoothScrollTo(mMenuWidth, 0); isOpen = false; } } public void toggle() { if (isOpen) { closeMenu(); } else { openMenu(); } } 

在 MainActivity 中呼叫 toggleMenu() 進行測試:

 public class MainActivity extends Activity { private SlidingMenu mMenu ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mMenu = (SlidingMenu) findViewById(R.id.id_menu); } public void toggleMenu(View view) { mMenu.toggle(); } } 

如此一來,點擊按鈕即可讓菜單平滑滑出或收起。將 padding 改為 100dp 後,菜單露出的比例也更合適。

3)加入 ListView 測試

把菜單改成 ListView 後測試也沒有衝突,大家可依自身需求自由修改。透過這個例子也能延伸做出更多縮放、透明度等動畫效果。

以上就是本文的全部內容,希望對大家的學習有所幫助。本文出自【張鴻洋的博客】。

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