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

Android 自訂對話框:把 AlertDialog 的樣子改成你要的

編輯:關於 Android 程式設計

系統原生的 AlertDialog 樣式很難直接套上品牌設計,實務上多半會自己寫版面配置,再塞進 Dialog 裡。下面把整個流程拆成幾段,順便整理幾種常見寫法。

一、先寫對話框的版面配置

新增 res/layout/dialog.xml,這裡示範一個帶單行輸入框的簡單樣式:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="2dip" >

        <EditText
            android:id="@+id/edittext1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:width="180dip" />
    </LinearLayout>

</LinearLayout>

二、把版面配置灌進 Builder

用 LayoutInflater 把 XML 轉成 View,再透過 setView() 交給 AlertDialog:

LayoutInflater inflater = LayoutInflater.from(EA6Activity.this);
final View dialogView = inflater.inflate(R.layout.dialog, null);

builder.setView(dialogView);

如果不需要保留 inflater 變數,也可以一行寫完:

View view = LayoutInflater.from(this).inflate(R.layout.dialog, null);

三、控制對話框的寬高

AlertDialog 預設寬度由系統主題決定,想自訂就透過它背後的 Window 調整:

WindowManager manager = getWindowManager();
Display display = manager.getDefaultDisplay();
int width  = display.getWidth();
int height = display.getHeight();

AlertDialog alert = new AlertDialog.Builder(DialogActivity.this).create();
alert.show();
alert.getWindow().setLayout(width / 2, height / 4);
alert.setTitle("測試");

四、另一種寫法:直接換掉 Dialog 的內容檢視

不透過 Builder.setView(),也可以先 show() 再指定內容版面,效果一樣,而且後續要抓元件、綁事件都方便:

final AlertDialog dlg = new AlertDialog.Builder(DialogActivity.this).create();
dlg.show();
Window window = dlg.getWindow();
// 關鍵就在這裡:換成自己定義的版面
window.setContentView(R.layout.dialog);

五、在自訂對話框裡綁定按鈕事件

因為元件已經掛在自己的版面上,用 window.findViewById() 就能取得:

mButton1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        final AlertDialog dlg = new AlertDialog.Builder(DialogActivity.this).create();
        dlg.show();
        Window window = dlg.getWindow();
        window.setContentView(R.layout.dialog);

        Button button1 = (Button) window.findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
    }
});

對應的按鈕版面(dialog.xml 之外也可獨立成檔),記得背景要換成自己的圖:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/icon1"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

實務提醒

自訂版面時,根元件的寬高建議用 wrap_content 搭配 setLayout() 控制,避免在小螢幕上被裁切。另外別忘了在 onCreateDialog() 與 onPrepareDialog() 的差異:前者只會在對話框首次建立時呼叫,若要每次顯示都重設狀態,該寫在後者。

作者:gongzibai

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