編輯:關於 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>
用 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("測試");
不透過 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
Android 平台電容式觸摸屏驅動原理解析
硬件工作原理 觸摸屏的工作原理,概括來說就是向上回報座標值,也就是 X 軸與 Y 軸的數值。因此在 Linux 中,它是透過 input 子系統來實現的。 本文主要歸納其
RecyclerView 滑動邊界檢測:打造可靠的頂部與底部判斷工具
從滑動監聽到邊界判斷 在 Android 開發中,RecyclerView 常用於長列表與動態資料展示。當我們需要實作「加載更多」、「回到頂部」、浮動按鈕的顯示與隱藏,或
Android 取得手機螢幕解析度的方法
在某些 Android 應用中,開發者可能需要取得裝置螢幕的寬度與高度,例如進行版面調整或顯示裝置資訊。本文示範如何透過 DisplayMetrics 取得手機螢幕解析度
Android 5.0 新特性 overview
Android 5.0 是 Google 於 2014 年 10 月 15 日(美國太平洋時間)發布的全新 Android 操作系統,英文名為 Lollipop,翻譯過來