編輯:關於 Android 程式設計
Android SDK 本身提供了建立選單的預設機制。這種機制在功能上相當完整,但外觀實在有點「土」。一個畫面絢麗的程式,配上一個樣式老氣的選單,整體觀感會顯得很突兀,甚至讓精心設計的介面大打折扣。對於如此靈活而強大的系統來說,改造選單樣式只是小事一樁。
為程式加上漂亮選單的方法有很多,本文先介紹最常用的一種:透過 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 則是三個選單項的圖示。
按下 Back 鍵時要處理兩件事:如果選單已彈出,就關閉選單;如果選單未彈出或已關閉,則直接結束目前的 Activity(呼叫 finish)。為了區分這兩種狀態,程式中放了一個 int 變數 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;
// 狀態變數:1 表示選單已彈出,2 表示未彈出
private int state = 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:
// 選單已彈出時不再開新視窗
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;
}
});
// 選單已彈出
state = 1;
return false;
case KeyEvent.KEYCODE_BACK:
if (state == 1) {
// 選單已彈出,先關閉它
pop.dismiss();
state = 2;
} else if (state == 2) {
// 選單未顯示或已關閉,直接結束 Activity
finish();
}
return false;
}
// 其他按鍵仍需交給父類別處理
return super.onKeyDown(keyCode, event);
}
}
一、點選後要記得關閉選單。系統選單的一般行為是「點選某項 → 執行動作 → 選單自動關閉」。為了模擬這個流程,替「首頁」加上單擊事件,點下去會先跳出一個 Toast 提示,接著呼叫 pop.dismiss() 並把狀態重設為 2。
二、處理完 Menu 與 Back 之後,不要再呼叫 super.onKeyDown。onKeyDown 只需回傳一個常量(true 或 false 皆可)。若又往下呼叫父類別實作,就會在執行完自訂動作後再跑一次系統預設行為——最典型的慘案就是按下 Back 關掉選單的同時,連 Activity 也被一起關閉了。相對地,對於 Menu 與 Back 以外的按鍵,仍必須呼叫 super.onKeyDown(),否則程式會變成「除了這兩個鍵,其他鍵都不太靈」。
三、showAtLocation 的四個參數。第一個參數是一個 View 物件,方法內部實際只需要呼叫它的 getWindowToken() 取得一個 IBinder;第二個參數決定彈出位置(本例設為螢幕底部);最後兩個參數分別是水平與垂直偏移量,本例皆為 0,因此浮層會精準貼齊螢幕最下方,也就是選單應該出現的位置。
四、PopupWindow 的尺寸決定遮罩範圍。這裡把寬高分別設為螢幕寬高,讓浮層覆蓋整個畫面,點擊空白處的觸摸事件才會落到浮層上;若尺寸只包住建議項本身,外部點擊就不會被攔下來,選單也容易變成「關不掉」。
Android 自定義元件:一次定義,多處引用
開發中常會遇到同一組控制項在好幾個頁面重複出現,例如「客服專線」這一列。與其每頁重畫一次,不如把它包成自定義元件。 一、先寫元件的外觀 檔案:lyt_customer_
EditText 好搭檔:TextInputLayout 浮動標籤控制項用法
一、這是什麼 TextInputLayout 是 Design Support Library 提供的一個輔助控制項,作用是讓 EditText 的提示文字在獲得焦點、開
自訂控件 TipView:仿 QQ 長按後的氣泡提示窗口
先來看效果圖: 之前使用手機 QQ 時,一直覺得這個氣泡提示窗口相當別緻,今天便將它大致實作了一遍。整體可以拆成六個部分來說明。 一、為 TipView 定義成員變數
Android 源碼編譯實錄:從 4.4.2 源碼到 emulator 啟動
折騰了很久,4.4.2 的源碼總算下載完成。如果你只是想了解編譯流程,其實用現成的環境會省事很多。整個源碼約 16G 出頭,其中 .repo 目錄佔了大頭,需要的話可以清