編輯:關於 Android 程式設計
做 Android 開發時,你一定遇過這個困擾:系統低版本的 Dialog 樣式老氣又醜,高版本雖然美觀一些,卻仍與公司 UI 設計師提供的效果圖有段距離。既然預設樣式滿足不了需求,那乾脆自己動手,讓 Dialog 的版面配置也能完全客製化。
先來看看最終呈現的效果:

是不是相當有質感?接下來就一步一步拆解實作流程。
首先,我們先建立一個可自訂的 Dialog 類別,並透過內部 Builder 類別來集中管理所有設定:
/** * 自定義帶兩個button的dialog * * @author Haipeng * * 2016-7-28 */ public class DefindedDialog extends Dialog { public DefindedDialog(Context context, int theme) { super(context, theme); } public DefindedDialog(Context context) { super(context); } public static class Builder { private Context context; private String title; private String message; private String positiveButtonText; private String negativeButtonText; private OnDedindedClickedListener onDedindedClickedListener; /** * * @param context * @param title * 標題 * @param message * 提示內容 * @param positiveButtonText * 確定按鈕的文本 * @param negativeButtonText * 取消按鈕 * @param onDedindedClickedListener * 兩個按鈕的監聽 */ public Builder(Context context, String title, String message, String positiveButtonText, String negativeButtonText, OnDedindedClickedListener onDedindedClickedListener) { super(); this.context = context; this.title = title; this.message = message; this.positiveButtonText = positiveButtonText; this.negativeButtonText = negativeButtonText; this.onDedindedClickedListener = onDedindedClickedListener; } /** * Create the custom dialog */ public DefindedDialog create() { final DefindedDialog dialog = new DefindedDialog(context, R.style.TwoButtonDialog);// 設置dialog的風格 // 設置dialog不可取消 點擊其他地方不能隱藏 dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(false); View layout = View.inflate(context, R.layout.two_button_dialog, null); // 設置內容 TextView tv_content = (TextView) layout .findViewById(R.id.two_btn_desc); if (TextUtils.isEmpty(message)) { tv_content.setVisibility(View.GONE); } else { tv_content.setVisibility(View.VISIBLE); tv_content.setText(message); tv_content.setMovementMethod(new ScrollingMovementMethod()); } // 設置標題 TextView tv_title = (TextView) layout .findViewById(R.id.two_btn_title); // View view = layout.findViewById(R.id.view); if (TextUtils.isEmpty(title)) { tv_title.setVisibility(View.GONE); // view.setVisibility(View.GONE); } else { // 間隔線 // view.setVisibility(View.VISIBLE); tv_title.setVisibility(View.VISIBLE); tv_title.setText(title); } // 將自定義的布局設置上去 dialog.setContentView(layout); // 設置確定按鈕的文字以及點擊事件 Button confirm_btn = ((Button) layout .findViewById(R.id.confirm_btn)); if (TextUtils.isEmpty(positiveButtonText)) { confirm_btn.setText("確定"); } else { confirm_btn.setText(positiveButtonText); } confirm_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { onDedindedClickedListener.onPositiveButtonClieked(dialog); dialog.dismiss(); } }); // 設置取消按鈕 Button cancle_btn = ((Button) layout.findViewById(R.id.cancle_btn)); if (TextUtils.isEmpty(negativeButtonText)) { cancle_btn.setText("取消"); } else { cancle_btn.setText(negativeButtonText); } cancle_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { onDedindedClickedListener.onNegativeButtonClieked(dialog); dialog.dismiss(); } }); // 設置dialog的寬度 WindowManager.LayoutParams params = dialog.getWindow() .getAttributes(); params.width = getScreenWidth(context) - dp2px(context, 50); dialog.getWindow().setAttributes(params); return dialog; } /** * 獲得屏幕寬度 * * @param context * @return */ public int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * dp轉px * * @param context * @param val * @return */ public int dp2px(Context context, float dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics()); } } /** * 當點擊確定的時候會調用onPositiveButtonClieked 當點擊取消的時候會調用onPositiveButtonClieked * * @author Haipeng * * 2016-7-28 */ public interface OnDedindedClickedListener { void onPositiveButtonClieked(DefindedDialog dialog); void onNegativeButtonClieked(DefindedDialog dialog); } }
這裡有幾個關鍵設計要留意:
Builder 的 create() 方法用到了一个自訂樣式,用來去除系統預設的 Dialog 背景並隱藏標題列:
重點說明:
為了讓呼叫端更省事,我們把常用情境都封裝成靜態方法:
/** * 創建Dialog的工具類 * * @author Haipeng * * 2016-7-28 */ public class DialogUtils { /** * 只要內容的dialog 兩個按鈕的文字設置為默認 不要標題 * * @param context * @param content * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String content, DefindedDialog.OnDedindedClickedListener listener) { return commonDialogTwoBtn(context, null, content, "確定", "取消", listener); } /** * 只要內容的dialog 兩個按鈕的文字設置為不默認 * * @param context * @param content * @param positiveName * @param negativeName * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String content, String positiveName, String negativeName, DefindedDialog.OnDedindedClickedListener listener) { return commonDialogTwoBtn(context, null, content, positiveName, negativeName, listener); } /** * 既要內容 又要標題的dialog 並且兩個按鈕的文字設置為默認 * * @param context * @param title * @param content * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String title, String content, DefindedDialog.OnDedindedClickedListener listener) { return commonDialogTwoBtn(context, title, content, "確定", "取消", listener); } /** * 既要內容 又要標題的dialog 並且兩個按鈕的文字不默認 * * @param context * @param title * @param content * @param positiveName * @param negativeName * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String title, String content, String positiveName, String negativeName, DefindedDialog.OnDedindedClickedListener listener) { if (TextUtils.isEmpty(content)) { return null; } if (listener == null) { return null; } DefindedDialog.Builder alert = new DefindedDialog.Builder(context, title, content, positiveName, negativeName, listener); DefindedDialog dialog = alert.create(); dialog.show(); return dialog; } }
這裡兩個按鈕的文案預設是「確定」與「取消」,當然你也可以傳入自訂文字,而標題則是可選的。
private Button warming; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); warming = (Button) findViewById(R.id.warming); warming.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.warming: OnDedindedClickedListener onDedindedClickedListener = new DefindedDialog.OnDedindedClickedListener() { @Override public void onPositiveButtonClieked(DefindedDialog dialog) { Toast.makeText(MainActivity.this, "確定", Toast.LENGTH_SHORT) .show(); } @Override public void onNegativeButtonClieked(DefindedDialog dialog) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT) .show(); } }; DialogUtils.commonDialogTwoBtn(this, "你確定要刪除該商品嗎?", onDedindedClickedListener); DialogUtils.commonDialogTwoBtn(this, "刪除", "你確定要刪除該商品嗎?", onDedindedClickedListener); DialogUtils.commonDialogTwoBtn(this, "你確定要刪除該商品嗎?", "確定", "取消", onDedindedClickedListener); DialogUtils.commonDialogTwoBtn(this, "刪除", "你確定要刪除該商品嗎?", "確定", "取消", onDedindedClickedListener); break; } }
版面檔裡只放了一顆按鈕,為了方便一次比較四種呼叫方式,這裡在按下按鈕後一次彈出了四個 Dialog。透過這樣的封裝,日後要在專案裡大量重複使用彈窗時,只要一行程式碼就能搞定,維護起來輕鬆許多。
如何替你的 Android APK 瘦身:實用的減重招式整理
APK 檔越來越大,已經是不爭的事實。Android 最初版本的時候,一個 App 的 APK 大約只有 2MB;到了現在,10MB 到 20MB 已是常態。造成這種爆炸
Android 總結之 WebView 與 Javascript 互相呼叫
在混合式 App 中,常會使用 WebView 呈現 H5 頁面。若需要讓原生 Android 與網頁 JavaScript 互相傳遞資料,可透過 addJavascri
Android 將資料寫入本地 Excel:使用 jxl 實作教學
需求背景在某些應用场景中,我們可能需要將程式內的資料匯出成 Excel 檔,方便後續統計或分享。Android 本身沒有內建 Excel 操作能力,因此常見做法是引入第三
Android 讓 SwipeRefreshLayout 首次進入自動刷新
Android 版知乎那種「一打開 App 就自動轉一下刷新球」的體驗,其實很好做。以下提供兩種寫法。 方法一:在 onWindowFocusChanged 中觸發