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

Android 自訂 Dialog:改主題、換版面、封裝成類別

編輯:關於 Android 程式設計

自訂對話框是開發中很常出現的需求。這裡示範的做法用到了 Android 的 shape 圖形資源,Android 的 drawable 種類相當豐富,後續再另篇整理。本篇的實作可以拆成三個步驟。

一、修改系統預設的 Dialog 樣式(風格與主題)
二、自訂 Dialog 的版面檔
三、封裝一個繼承自 Dialog 的類別(或直接用 Dialog 類別),為了後續重複使用,建議封裝

原始碼下載:https://download.csdn.net/detail/wwj_748/7261031

效果圖:

步驟一:定義主題樣式

res/values/styles.xml:

<!-- 對話框主題 -->
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
</style>

把 windowBackground 設為透明、隱藏標題列,是讓自訂版面能完全掌控外觀的兩個關鍵設定。若還想讓對話框浮在背景之上、背景變暗,可以再補上 android:windowBackgroundDimEnabled 與 android:backgroundDimAmount。

步驟二:封裝 CustomDialog

package com.wwj.custom.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

/**
 * 自訂對話框
 */
public class CustomDialog extends Dialog {
    private static int default_width = 160;  // 預設寬度(dp)
    private static int default_height = 120; // 預設高度(dp)

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

    public CustomDialog(Context context, int layout, int style) {
        this(context, default_width, default_height, layout, style);
    }

    public CustomDialog(Context context, int width, int height, int layout, int style) {
        super(context, style);
        // 設定內容版面
        setContentView(layout);

        // 設定視窗屬性
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();

        // 寬高需換算成實際像素,否則在不同密度裝置上會失真
        float density = getDensity(context);
        params.width = (int) (width * density);
        params.height = (int) (height * density);
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    /** 取得顯示密度 */
    public float getDensity(Context context) {
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        return dm.density;
    }
}

步驟三:自訂版面

res/layout/dialog_layout.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/dialog_bg"
    android:gravity="center"
    android:orientation="vertical" >
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="正在執行..." />
</LinearLayout>

版面用到的背景資源 res/drawable/dialog_bg.xml,用 shape 畫出圓角半透明底:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="10dp" />
    <solid android:color="#55000000" />
</shape>

步驟四:顯示對話框

package com.wwj.custom.dialog;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomDialog customDialog =
            new CustomDialog(this, R.layout.dialog_layout, R.style.DialogTheme);
        customDialog.show();
    }
}

幾個實務上的補充

用 shape 畫圓角矩形背景,比放一張 .9.png 圖片更靈活:改 corners 的半徑就能調圓角,改 solid 的色碼就能調透明度,不需要重新切圖。若想加上邊框,再補一個 <stroke> 即可。

另外,封裝時把寬高設計成 dp 單位並在內部換算成 px,是為了讓同一套程式碼在不同解析度裝置上都有一致的外觀。最後別忘了在 Activity 銷毀時呼叫 dismiss(),否則畫面旋轉或 Activity 被回收時,容易出現 WindowLeaked 的錯誤日誌。

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