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

Android 自訂 Dialog:擺脫系統樣式的定制做法

編輯:關於 Android 程式設計

有些特殊場景下,系統 Dialog 提供的樣式無法滿足需求,這時候就得自己定制。以下是一個基礎範例(靈感來自 Stack Overflow),你可以在此之上繼續改造。

自訂版面:custom_dialog.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:background="#3E80B4"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="Do you really want to exit?"
        android:textColor="@android:color/white"
        android:textSize="15dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#3E80B4"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:background="@android:color/white"
            android:text="Yes"
            android:textColor="#5DBCD2" />

        <Button
            android:id="@+id/btn_no"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="5dp"
            android:background="@android:color/white"
            android:text="No"
            android:textColor="#5DBCD2" />
    </LinearLayout>
</LinearLayout>

CustomDialog 類別

public class CustomDialogClass extends Dialog
        implements android.view.View.OnClickListener {

    public Activity c;
    public Dialog d;
    public Button yes, no;

    public CustomDialogClass(Activity a) {
        super(a);
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);

        yes = (Button) findViewById(R.id.btn_yes);
        no = (Button) findViewById(R.id.btn_no);
        yes.setOnClickListener(this);
        no.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_yes:
                c.finish();
                break;
            case R.id.btn_no:
                dismiss();
                break;
            default:
                break;
        }
    }
}

關鍵點是繼承 Dialog 而非 AlertDialog,這樣就完全擺脫了系統樣式的束縛,版面完全由你的 XML 決定。requestWindowFeature(Window.FEATURE_NO_TITLE) 用來去掉系統標題欄,它必須在 setContentView 之前呼叫。

呼叫方式

CustomDialogClass cdd = new CustomDialogClass(Values.this);
cdd.show();

示範程式碼的幾個問題與改進建議

1. 建構子直接呼叫 Activity.finish() 是個糟糕的設計——一個通用 Dialog 元件不該硬編碼「關閉整個頁面」的業務邏輯。應該改成透過回呼介面把使用者選擇傳出去,由呼叫方決定要怎麼處理。

public interface OnDialogButtonClickListener {
    void onPositiveClick();
    void onNegativeClick();
}

// 使用時
new CustomDialogClass(this, new OnDialogButtonClickListener() {
    @Override public void onPositiveClick() { finish(); }
    @Override public void onNegativeClick() { }
}).show();

2. 示範中的 onClick 原本在 switch 之後又呼叫了一次 dismiss(),會導致點 Yes 時先 finish 再 dismiss,行為不直觀。改寫後只在 Negative 分支關閉即可。

3. public Activity c 這種寫法暴露了內部欄位,應改為 private 並透過建構子注入。

4. 版面中的 dp 與顏色寫死在 XML 裡,若有多個 Dialog 需要共用風格,建議抽出到 styles.xml 與 colors.xml。

5. 現代寫法建議改用 DialogFragment(或已棄用的 DialogFragment 的替代品)。原因有兩點:一是 Dialog 持有 Activity 的強引用,在螢幕旋轉時容易造成 window leak;二是 DialogFragment 由 FragmentManager 托管,能自動處理重建與生命週期,不必自己操心。

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