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

Android 中應用界面主題 Theme 的使用方法和頁面定時跳轉應用

編輯:關於 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>

三、只套用到單一 Activity

在 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 手寫版本容易漏掉的狀態。

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