編輯:Android 開發實例
Android SDK 本身提供了一套建立菜單的預設機制,這種方式做出來的菜單功能上雖然完整,但外觀實在有點「土」。當一個畫面絢麗的應用配上一個老氣的菜單,整體質感就會大打折扣,讓使用者覺得很突兀。好在 Android 系統既靈活又強大,想改寫菜單樣式其實只是小事一場。本文先介紹一種常見做法——透過 onKeyDown 事件搭配 PopupWindow 來打造自定義菜單。至於能否做出華麗的效果,就要看設計、美學與使用者心理的功力了。
眾所周知,按下機身上的「Menu」硬鍵(不同機型位置略有差異)會彈出選項菜單,再按「Back」鍵則會關閉。想模擬這套彈出與關閉行為,只需監聽這兩個按鍵的按下事件,並在按下「Menu」時用 PopupWindow 彈出一個視窗當作模擬的選項菜單即可。先來看看模擬後的效果。

從圖中可以看到,畫面下方顯示了「首頁」、「我的」和「更多」三個菜單項。其中「我的」的文字與圖示是左右水平排列,另外兩個則是上下垂直排列。這樣的效果其實只需一個普通的佈局檔(menu_layout.xml)就能完成,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="bottom">
<!-- 第一個菜單項:"首頁" -->
<LinearLayout android:id="@+id/home" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/button_normal_translucent"
android:layout_weight="1">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:src="@drawable/home"
android:paddingTop="5dp" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="首頁"
android:gravity="center" />
</LinearLayout>
<!-- 第二個菜單項:"我的" -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/button_normal" android:layout_weight="1"
android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/mine"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="我的" />
</LinearLayout>
<!-- 第三個菜單項
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="@drawable/button_normal"
android:layout_weight="1">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:src="@drawable/more"
android:paddingTop="18dp" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="更多"
android:gravity="center" android:paddingTop="5dp"/>
</LinearLayout>
</LinearLayout>
撰寫上述代碼前,別忘了先準備好相關圖片。本例共用到 5 張圖:button_normal_translucent.png 作為「首頁」菜單項的半透明背景,button_normal.png 作為「我的」與「更多」的背景,home.png、mine.png 與 more.png 則分別是這三個菜單項的圖示。
接著來撰寫監聽「menu」與「back」鍵按下動作的代碼。按下「back」時要處理兩件事:若選項菜單已彈出,就關閉它;若未彈出或已關閉,則直接呼叫 finish 關閉目前的 Activity。為了區分這兩種情況,程式中設了一個 int 型別的狀態變數 state,state 為 1 表示選項菜單已彈出,為 2 表示未彈出。完整實作如下:
package mobile.android.ch06.custom.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.Toast;
public class Main extends Activity
{
private PopupWindow pop;
private View layout;
private int state = 2; //狀態變量,1:選項菜單已彈出,2:選項菜單未彈出
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_MENU: // 按下"menu"鍵的動作
// 選項菜單已彈出,不再彈出新的窗口
if (state == 1)
return false;
// 裝載選項菜單布局文件
layout = getLayoutInflater().inflate(R.layout.menu_layout, null);
// 創建PopupWindow對象,並在指定位置彈出用於顯示菜單的窗口
pop = new PopupWindow(layout, getWindowManager()
.getDefaultDisplay().getWidth(), getWindowManager()
.getDefaultDisplay().getHeight());
// 設置彈出窗口的位置
pop.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
View home = layout.findViewById(R.id.home);
// 為"首頁"菜單項添加單擊事件
home.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(Main.this, "單擊定制菜單.", Toast.LENGTH_LONG).show();
// 單擊"首頁"菜單項後,關閉選項菜單
pop.dismiss();
// 重新設置狀態變量
state = 2;
}
});
// 彈出選項菜單後,將狀態變量設為1,表示選項菜單已彈出
state = 1;
return false;
case KeyEvent.KEYCODE_BACK: // 按下"back"鍵的動作
if (state == 1)
{
// 如果選項菜單已彈出,關閉它
pop.dismiss();
// 將狀態變量設為選項菜單已關閉
state = 2;
}
else if (state == 2)
{
// 如果選項菜單還沒有顯示,或已經關閉,則直接關閉當前的Activity
finish();
}
return false;
}
// 除"menu"和"back"按下事件外,仍需調用Activity類的onKeyDown方法來響應其他鍵的按下事件
return super.onKeyDown(keyCode, event);
}
}
撰寫上述代碼時應注意幾個要點。首先,選項菜單一般單擊某項後會執行動作並自動關閉,為了模擬這個過程,本例為「首頁」菜單項加了單擊事件:點擊後會彈出 Toast 提示,同時關閉菜單。其次,執行完「menu」或「back」的按下動作後,onKeyDown 應回傳一個常量(false 或 true 皆可),且不能再呼叫 super.onKeyDown,否則做完自定義動作後又會觸發系統預設行為——例如按「back」關閉菜單後連 Activity 也一併關掉。當然,若是「menu」和「back」以外的按鍵,仍需呼叫 Activity 的 onKeyDown(即 super.onKeyDown),這樣程式才能繼續響應其他按鍵,否則除了這兩個鍵之外幾乎都不管用。最後,showAtLocation 方法用於指定彈出視窗的位置:第一個參數是 View 物件,內部其實只需呼叫 View.getWindowToken 取得一個 IBinder;第二個參數決定彈出位置,本例設為畫面底部;最後兩個參數是水平與垂直偏移量,本例皆設為 0,因此彈出視窗會顯示在螢幕最底部,也就是選項菜單應該出現的位置。
Android PreferenceActivity 詳解:用系統設定頁的架構做自己的設定介面
很多書對 PreferenceActivity 幾乎一筆帶過,但它在真實專案裡非常好用。本文把它的資料保存機制、建立方式、可用元素與事件回呼一次講完。 先看長相 左圖
Android 登入畫面範例:帳號密碼與錯誤次數限制
登入畫面常用來驗證使用者身份。實作時通常需要兩個輸入欄位:使用者名稱與密碼,並加上登入按鈕。為了避免暴力嘗試,還可以限制錯誤次數。 建立輸入欄位 <EditText
Android 本地化教學:讓 App 依地區顯示對應語言
Android 應用會運行在不同語言環境的裝置上。要讓應用更具親和力,就必須讓文字、數字與檔案等资源能配合裝置的語言環境自動調整。本章說明如何依不同區域(Locale)本
Android 模仿 UCWeb 菜單效果:PopupWindow 搭配 GridView 實作
UCWeb 那個半透明、置底彈出的九宮格菜單看起來很不錯,自己做一個也不難。整體思路是:保留實體 Menu 鍵的作用(讓它負責彈出菜單)用 PopupWindow 當菜單