編輯:關於 Android 程式設計
對話框在 Android 開發中隨處可見。本文將結合實際項目,從 AlertDialog 的使用、Dialog 的使用,以及兩者的異同與選擇三個方面,做一個系統性的梳理。
在 Android 源碼中,AlertDialog 的構造被保護起來,無法直接 new,但提供了內部類 Builder,通過它即可完成各種對話框效果。
一個簡單的問卷調查:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.icon_title);
builder.setTitle("問卷調查");
builder.setMessage("喜歡我嗎?");
builder.setPositiveButton("喜歡", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "謝謝~", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("特喜歡", /* ... */null);
builder.setNeutralButton("超喜歡", /* ... */null);
builder.show();
Builder 提供了 setIcon()、setTitle()、setMessage()、setPositiveButton()、setNegativeButton()、setNeutralButton() 等方法可供選擇。
使用 setItems() 可實現單選列表:
final String[] items = {"1", "2", "3", "4"};
builder.setIcon(R.drawable.icon_title);
builder.setTitle("請選擇");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, items[i], Toast.LENGTH_SHORT).show();
}
});
builder.show();
通過 setSingleChoiceItems() 實現,第二個參數為默認選中項索引:
final String[] strings = {"男", "女", "其他"};
builder.setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, strings[i], Toast.LENGTH_SHORT).show();
}
});
builder.show();
復選框通過 setMultiChoiceItems() 實現:
final String[] strings = {"籃球", "足球", "乒乓球", "羽毛球"};
builder.setMultiChoiceItems(strings, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int i, boolean b) {
Toast.makeText(MainActivity.this, "我喜歡" + strings[i], Toast.LENGTH_SHORT).show();
}
});
builder.show();
想要更美觀的彈出框,就必須自定義布局,通過 setView() 設置。以下是一個仿微信的菜單彈窗:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view, null);
v.findViewById(R.id.tv_no_read).setOnClickListener(this);
v.findViewById(R.id.tv_top_one).setOnClickListener(this);
v.findViewById(R.id.tv_del).setOnClickListener(this);
builder.setView(v);
AlertDialog alertDialog = builder.create();
alertDialog.show();
默認情況下彈窗寬度未必美觀,可以通過 Window 的 LayoutParams 調整寬度為屏幕的 0.66:
AlertDialog dialog = builder.create();
dialog.show(); // 注意:必須先 show() 再設置 params,否則不生效
Window window = dialog.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = (int) (getDisply().widthPixels * 0.66);
window.setAttributes(params);
這裡有幾點要點:
通過 Builder 的 create() 拿到的 dialog,必須先調用 show() 再設置 params,否則參數無效;dialog 通過 getWindow() 拿到 window,再通過 getAttributes() 拿到參數並賦值,最後用 setAttributes() 生效。
若還想調整彈窗位置與透明度,可設置 Gravity 與 alpha:
Window w = alertDialog.getWindow();
WindowManager.LayoutParams attributes = w.getAttributes();
w.setGravity(Gravity.LEFT | Gravity.TOP);
attributes.width = (int) (getDisply().widthPixels * 0.66);
attributes.alpha = 0.6f;
w.setAttributes(attributes);
系統默認 Gravity 為 Gravity.CENTER。設置 Gravity 只是確定大致方位,若要進一步微調,還需改變 x、y 坐標:
attributes.x = 100; // X 坐標偏移
attributes.y = 100; // Y 坐標偏移
w.setAttributes(attributes);
關於 x、y 的含義需注意:x 與 y 表示相對於原始位置的偏移。當含 Gravity.LEFT 時 x 表示相對左邊的偏移(負值忽略),含 Gravity.RIGHT 時表示相對右邊的偏移;含 Gravity.CENTER_HORIZONTAL 時,正值右移、負值左移;含 Gravity.CENTER_VERTICAL 時同理,正值下移、負值上移。
Dialog 的用法與 AlertDialog 大致相同,因此比較簡單。實現仿微信 Dialog(帶標題):
Dialog dialog = new Dialog(MainActivity.this);
dialog.setTitle("操作");
// dialog.setCancelable(false); // 設置是否可點擊外部取消,默認 true
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view, null);
v.findViewById(R.id.tv_no_read).setOnClickListener(this);
// ...
dialog.setContentView(v); // Dialog 只有 setContentView(),沒有 setView()
dialog.show();
需要注意:
Dialog 默認有 title,若想去掉 title 可採用自定義 Theme;Dialog 只有 setContentView() 方法;且 Dialog 不存在 AlertDialog 那種「先 create 再 show」的問題,設置完參數後再調 show() 沒有問題。
實現不帶標題的仿微信 Dialog,可通過自定義 Theme:
Dialog dialog = new Dialog(MainActivity.this, R.style.my_dialog);
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view, null);
dialog.setContentView(v);
dialog.show();
my_dialog 主題定義:
<style name="my_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item> <!--邊框-->
<item name="android:windowIsFloating">true</item> <!--是否浮動-->
<item name="android:windowNoTitle">true</item> <!--無標題-->
<item name="android:windowBackground">@color/transparent</item> <!--背景透明-->
<item name="android:backgroundDimEnabled">false</item> <!--不變暗-->
</style>
Dialog 的位置、大小等屬性與 AlertDialog 基本一致,不再贅述。自定義一個仿 QQ 底部菜單的 Dialog,只需結合 Window 的 Gravity 與 x、y 坐標即可實現右上角彈出:
CustomDialogView dialog = new CustomDialogView(this, R.style.my_dialog);
Window win = dialog.getWindow();
WindowManager.LayoutParams p = win.getAttributes();
win.setGravity(Gravity.RIGHT | Gravity.TOP);
p.x = 20;
p.y = 130;
win.setAttributes(p);
dialog.setCanceledOnTouchOutside(true); // 點擊外部關閉
dialog.show();
總結而言:
1. Dialog 默認有 title,可 setContentView(),但沒有內建按鈕;
2. AlertDialog(Builder)默認沒有 title,內建多按鈕、setSingleChoiceItems() 單選框、setMultiChoiceItems() 復選框,並可通過 setView() 自定義布局。
因此,若需要標準的「標題 + 消息 + 按鈕」或列表選擇,直接用 AlertDialog.Builder 最省事;若追求高度自定義的外觀與交互,則用 Dialog 配合自定義 Theme 更靈活。Dialog 的總結就到這裡,乾貨會持續不斷地更新。
機器冰冷仍有溫情:遠程協助玩轉 Android 手機
許多年長的長輩和不太熟悉手機的朋友,在操作 Android 手機時總會遇到「卡關」問題,例如某個軟體不會設置、某項功能無法開啟等。那麼身在異地的我們又該如何協助他們呢?在
ImageView 的 ScaleType 全解析:八種縮放模式的真實差異
ScaleType 決定了一張圖片在 ImageView 內部「如何縮放、如何對齊」。同樣一張圖、同樣一個 150dp × 100dp 的容器,換一個 ScaleType
Android 並發編程(一):程序的啟動與終結
Android 是一個多用戶、多任務的系統,允許眾多 App 同時運行,並在彼此之間快速切換。這一切能力的基礎,是 Linux 內核提供的進程機制。 Linux 進程
Android 源碼 URLUtil 文件名解析問題排查
今天修復了一個使用系統方法 android.webkit.URLUtil.guessFileName(String url, String contentDisposit