AlertDialog有很多用法
下面提供一些比較進階的問題
Q1:如何讓Dialog不會被back鍵 及 觸碰空白處取消
A:(參考)
//AlertDialog builder物件 |
18 |
AlertDialog.Builder builder = newAlertDialog.Builder(this); |
19 |
|
20 |
//設定AlertDialog內容 |
21 |
builder.setTitle("Title") |
22 |
.setMessage("要顯示的訊息") |
23 |
.setPositiveButton("OK", null) |
24 |
.setNegativeButton("Cancel", null) |
25 |
.setCancelable(false); //設定back鍵無法dismiss dialog |
26 |
|
27 |
//建立AlertDialog |
28 |
AlertDialog alert = builder.create(); |
29 |
//顯示AlertDialog |
30 |
alert.show(); |
Q2:如何自定義AlertDialog & 如何在AlertDialog內放置元件
A:參考 http://cookiesp.pixnet.net/blog/post/77342445
A2:參考http://www.dotblogs.com.tw/superlm102/archive/2013/02/05/90118.aspx
Q3:如何產生橫的AlertDialog
A:參考
//產生dialog
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Test Dialog")
.setMessage("This should expand to the full width")
.show();
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
Q4:如何自定AlertDialog的位置
A:
AlertDialog dlg = new AlertDialog.Builder(context).create(); LayoutInflater flater = LayoutInflater.from(context); View view = flater.inflate(R.layout.mode_tv_layout2_gridview_dialog, null); dlg.setView(view); dlg.show(); 方法一 Window window = dlg.getWindow(); window.setGravity(Gravity.BOTTOM);//BOTTOM為底部 可自選 方法二 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); Window window = dlg.getWindow(); lp = window.getAttributes(); lp.alpha = 0.5f;//透明度 lp.y = 100;//高度 window.setAttributes(lp);
Q5:
文章標籤
全站熱搜
