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

Android 自訂 Dialog 三步驟實作教學

編輯:關於 Android 程式設計

寫 Android 應用幾乎一定會碰到「系統預設 Dialog 樣式跟自家設計稿搭不上」的狀況。還好自訂 Dialog 只要三個步驟:

  1. 撰寫繼承 Dialog 的 Java 類別。
  2. 準備自訂版面檔,並在 styles.xml 中定義對應的 Theme。
  3. 在需要的地方建立並顯示。

步驟一:建立 CustomPopDialog2

這個範例的效果是:中央彈出一張圖片(例如 QR Code),點擊 Dialog 以外的區域自動關閉。

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;

/**
 * 中央顯示圖片、點擊外部區域關閉的自訂 Dialog
 */
public class CustomPopDialog2 extends Dialog {

    public CustomPopDialog2(Context context) {
        super(context);
    }

    public CustomPopDialog2(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private Bitmap image;

        public Builder(Context context) {
            this.context = context;
        }

        public Bitmap getImage() {
            return image;
        }

        public void setImage(Bitmap image) {
            this.image = image;
        }

        public CustomPopDialog2 create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomPopDialog2 dialog = new CustomPopDialog2(context, R.style.Dialog);

            View layout = inflater.inflate(R.layout.dialog_share_qrcode, null);
            dialog.addContentView(layout, new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);

            ImageView img = (ImageView) layout.findViewById(R.id.img_qrcode);
            img.setImageBitmap(getImage());
            return dialog;
        }
    }
}

這裡的關鍵是 create():把自訂版面 inflate 出來、塞進 Dialog、再把資料填進去。用 Builder 模式包裝,呼叫端寫起來會乾淨很多。

步驟二:版面檔與 Theme

<!-- res/layout/dialog_share_qrcode.xml -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_qrcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="二維碼" />

</LinearLayout>

Theme 的重點是把系統 Dialog 的灰色背景與標題列拿掉,讓自訂版面完全接管:

<!-- res/values/styles.xml -->
<style name="Dialog" parent="android:style/Theme.Dialog">
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
</style>

步驟三:使用

Bitmap bitmap = xxxxx;   // 取得要顯示的圖片,也可替換成其他參數

CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context);
dialogBuild.setImage(bitmap);
CustomPopDialog2 dialog = dialogBuild.create();

dialog.setCanceledOnTouchOutside(true);   // 點擊外部區域關閉
dialog.show();

最終效果

幾個實務補充

  • 記得在 onDestroy() 或畫面離開時呼叫 dismiss(),避免 Activity 已銷毀卻仍持有 Dialog 造成 WindowLeaked。
  • 若 Dialog 內有輸入框,記得在 AndroidManifest 或 Window 上處理 adjustResize / adjustPan,否則鍵盤會把輸入框蓋住。
  • 樣式較複雜時,與其繼承 Dialog,不如直接用 DialogFragment(或 DialogFragmentCompat)——它會自動跟著 Fragment 生命週期管理,旋轉螢幕與背景重設時也不會出錯。
  • 若只是彈一個簡單卡片,BottomSheetDialog 或 PopupWindow 往往比自訂 Dialog 更省事。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved