編輯:關於 Android 程式設計
Theme 用來決定界面的整體 UI 風格,可以套在整個應用程式,也可以只套在某個 Activity 上。本文整理系統內建主題清單、兩種套用範圍,以及三個實作上很常用的小技巧。

Android SDK 內建的主題可依「標題列 Title Bar」與「狀態列 Status Bar」是否可見來分類:
android:theme="@android:style/Theme.Dialog" 對話框模式 android:theme="@android:style/Theme.NoTitleBar" 不顯示標題列 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 無標題列並全螢幕 android:theme="Theme.Light" 白色背景 android:theme="Theme.Light.NoTitleBar" 白底無標題列 android:theme="Theme.Light.NoTitleBar.Fullscreen" 白底、無標題列、全螢幕 android:theme="Theme.Black" 黑色背景 android:theme="Theme.Black.NoTitleBar" 黑底無標題列 android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑底、無標題列、全螢幕 android:theme="Theme.Wallpaper" 以系統桌布作背景 android:theme="Theme.Wallpaper.NoTitleBar" 桌布背景、無標題列 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 桌布背景、無標題列、全螢幕 android:theme="Translucent" 半透明 android:theme="Theme.Translucent.NoTitleBar" 半透明、無標題列 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、無標題列、全螢幕 android:theme="Theme.Panel" android:theme="Theme.Light.Panel"
在 AndroidManifest.xml 的 application 節點上設定 theme 屬性:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar">
</application>
在 AndroidManifest.xml 中設定:
<activity
android:name=".About"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
在 Java 程式碼中設定:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
setContentView(R.layout.main);
}
這裡有個關鍵時機:setTheme() 必須在 setContentView() 之前呼叫,因為主題會在版面被膨脹的那一刻套用。寫在後面等於沒寫。
歡迎頁停留幾秒再自動進入主頁,是最常見的 Theme 搭配場景(通常再配上 Theme.NoTitleBar.Fullscreen 讓開屏畫面更乾淨):
public void Start() {
new Thread() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setClass(WelComeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}.start();
}
用 Thread.sleep 做倒數是最直白的寫法,缺點是計時期間完全無法取消,Activity 若被提早銷毀仍會嘗試跳轉。正式環境建議改用 Handler.postDelayed() 並在 onDestroy() 中 removeCallbacks(),就能避免這個問題。
imageButton1 = (ImageButton) findViewById(R.id.imageButton3);
imageButton1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.drawable.menu_btn_f); // 按下
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.setBackgroundResource(R.drawable.menu_btn); // 放開
}
return false;
}
});
這個做法直觀,但更推薦用 selector 宣告式地描述狀態與圖片的對應關係,把判斷交給系統。除了程式碼更短,selector 也能自動處理 ACTION_CANCEL(手勢被系統中斷)等 this 手寫版本容易漏掉的狀態。
Android 常用的優秀開源框架大盤點
本文對 Android 開源框架庫進行分類,挑選出最常用、最實用的開源專案。本篇主要介紹優秀的開源框架庫與專案,UI 個性化控制件會另行介紹。 依賴注入(Dependen
Android 實用工具介紹:emulator 模擬器怎麼開
在 android-sdk\tools 目錄下有一個名為 emulator.exe 的可執行檔。望文生義,它就是 Android 的模擬器本體。但不少人會遇到一個奇怪的現
Android 總結之 WebView 與 Javascript 互相呼叫
在混合式 App 中,常會使用 WebView 呈現 H5 頁面。若需要讓原生 Android 與網頁 JavaScript 互相傳遞資料,可透過 addJavascri
Android 手機黑名單的實現:來電攔截與短信屏蔽
有了前面幾篇關於 AIDL 與自動掛斷電話的鋪墊,這次把黑名單這個完整功能做出來。用過 Android 的都熟悉:把某些號碼拉黑之後,對方打電話進不來、發短信也不會提示。