編輯:關於 Android 開發
專案做久了就會發現,系統預設的跳脫視窗實在太制式,永遠是「標題 + 訊息 + 兩個按鈕」那套。當設計稿要求圓角、自訂間距、雙按鈕不同樣式時,就必須自己來做一個 Dialog。
整體結構是「上方訊息區 + 分隔線 + 下方按鈕區」的垂直排列,並用 shape 資源做出圓角背景。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/shape_linearlayout"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="TextView" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/line" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="1dp" >
<Button
android:id="@+id/btn_dialog_go"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/linearlayout"
android:text="確定"
android:textColor="@color/blue" />
<View
android:id="@+id/dialog_bg"
android:layout_width="1dp"
android:layout_height="40dp"
android:background="@color/line" />
<Button
android:id="@+id/btn_dialog_add"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/linearlayout"
android:text="取消"
android:textColor="@color/blue" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
兩個按鈕都用 layout_width="0dp" 搭配 layout_weight="1" 等分寬度,這是 LinearLayout 裡讓子元件均分空間的標準寫法。
工具類採單例模式,外部只需四個步驟就能使用。
public class DialogUtil {
private static DialogUtil dialogUtil;
private Dialog dialog;
private TextView message;
private Button firstBtn;
private Button secondBtn;
private onButtnClickListener buttnClickListener;
public static DialogUtil getDialogInstance(Context context) {
if (null == dialogUtil) {
dialogUtil = new DialogUtil(context);
}
return dialogUtil;
}
private DialogUtil(Context context) {
initDialog(context);
}
public void setMessage(String message) {
if (null != message && !message.equals("")) {
this.message.setText(message);
}
}
private void initDialog(Context context) {
dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
Window window = dialog.getWindow();
dialog.setCanceledOnTouchOutside(true);
WindowManager.LayoutParams params = window.getAttributes();
// 寬度設為螢幕的四分之三
params.width = ScreenUtils.getScreenWidth(context)
- ScreenUtils.getScreenWidth(context) / 4;
params.height = LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
window.setAttributes(params);
window.setWindowAnimations(R.style.displayStyle);
message = (TextView) dialog.findViewById(R.id.txt_dialog_message);
firstBtn = (Button) dialog.findViewById(R.id.btn_dialog_go);
secondBtn = (Button) dialog.findViewById(R.id.btn_dialog_add);
firstBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != buttnClickListener)
buttnClickListener.onFirstBtnClick(v);
}
});
secondBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != buttnClickListener)
buttnClickListener.onSecondBtnClick(v);
}
});
}
public void showDialog() {
if (dialog != null && !dialog.isShowing()) {
dialog.show();
}
}
public void dissmissDialog() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
public void setOnButtnClickListener(onButtnClickListener buttnClickListener) {
this.buttnClickListener = buttnClickListener;
}
public interface onButtnClickListener {
public void onFirstBtnClick(View v);
public void onSecondBtnClick(View v);
}
}
整個使用方式就四行,順序不能亂:
// 1、取得物件
DialogUtil dialogUtil = DialogUtil.getDialogInstance(context);
// 2、設定提示文字
dialogUtil.setMessage("確定要刪除這筆資料嗎?");
// 3、設定按鈕點擊事件
dialogUtil.setOnButtnClickListener(new DialogUtil.onButtnClickListener() {
@Override
public void onFirstBtnClick(View v) {
// 確定
}
@Override
public void onSecondBtnClick(View v) {
// 取消
}
});
// 4、顯示 dialog
dialogUtil.showDialog();
優點是調用極簡潔,樣式集中管理,全專案視覺一致,改一次全站生效。
缺點也必須誠實指出:單例長期持有 Context 容易造成記憶體洩漏,特別是若傳入的是 Activity 而非 ApplicationContext;而且單例意味著同一時間只有一個 Dialog 實例,無法同時彈出多個不同內容的視窗。
更穩健的替代方案是把 DialogUtil 改成「每次 new 一個新實例」的一般工具類,或是直接封裝成 DialogFragment。後者能自動跟隨 Activity 生命週期,旋轉螢幕時不會造成視窗洩漏,是目前 Android 官方推薦的做法。若只是小型專案或 Demo,本文的單例寫法依然堪用。
Android WebView 遠端程式碼執行漏洞成因剖析
先看問題場景 本文以一個公開的示範專案(AppVulnerability / WebViewFileDemo)為例,分析 Android WebView 中「遠端程式碼執
Android 安全攻防戰(下):反編譯與代碼混淆技術完全解析
Android 安全攻防戰(下):反編譯與代碼混淆技術完全解析 上一篇我們學習了 Android 程序反編譯的知識,包括反編譯代碼、反編譯資源以及重新打包。由此可見,我們
Android 各種 Span 詳解:打造豐富多彩的 TextView 文本樣式
在 android.text.style 包下,提供了大量 Span 類,幫助我們實現 TextView 中的特殊文本效果,例如讓部分內容呈現不同的顏色、字體、大小,甚至
動手寫一個 Android JsBridge:借鑑 Binder 思路打通 Java 與 JavaScript
什麼是 JsBridge Android JsBridge 是用來在原生 Java 代碼與 JavaScript 代碼之間架設通信橋梁的輔助工具。混合開發的 App 裡,