2010-12-10 53 views
44
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Title"); 
     builder.setItems(items, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     AlertDialog alert = builder.create(); 

我正在使用上面的代碼來顯示一個警報對話框。默認情況下,它以寬度填充屏幕並以高度填充wrap_content。
如何控制默認警報對話框的寬度和高度?
我想:如何在Android中控制默認警報對話框的寬度和高度?

alert.getWindow().setLayout(100,100); // It didn't work. 

如何獲得提示窗口布局PARAMS和手動設置的寬度和高度?

回答

148

只有在週六代碼略有變化,設置後alertDialog的show()方法的佈局。

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(layout); 
builder.setTitle("Title"); 
alertDialog = builder.create(); 
alertDialog.show(); 
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height. 

或者你可以用我的方式做到這一點。

alertDialog.show(); 
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 

lp.copyFrom(alertDialog.getWindow().getAttributes()); 
lp.width = 150; 
lp.height = 500; 
lp.x=-170; 
lp.y=100; 
alertDialog.getWindow().setAttributes(lp); 
4

我不知道你是否可以改變AlertDialog的默認高度/寬度,但如果你想這樣做,我想你可以通過創建自己的自定義對話框來實現。您只需在android manifest.xml中爲您的活動提供android:theme="@android:style/Theme.Dialog",並可根據您的要求編寫整個佈局。您可以從Android資源XML中設置自定義對話框的高度和寬度。

+0

它可能工作,但我不想添加其他活動。我發佈了我使用的解決方案。謝謝。 – sat 2010-12-15 07:21:55

+0

是的,上述方法也適用於在彈出式樣式中顯示項目或說警報對話框樣式。剛剛在其中一個項目中嘗試過。 – sat 2010-12-15 10:28:20

+0

剛剛比較了兩種方法,1.使用AlertDialog.builder和另一個2.使用主題Dialog的活動,第二個是更好的解決方案,只有當需要將一些結果發回背景視圖時纔有缺點,必須使用startActivityForResult,因爲元素(視圖)在後臺活動不可用。 – sat 2010-12-16 11:11:30

24

好吧,我可以使用Builder類控制寬度和高度。 我用

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(layout); 
builder.setTitle("Title"); 
AlertDialog alertDialog = builder.create(); 
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height. 
alertDialog.show(); 
7

該作品,以及通過增加.getWindow().setLayout(width, height)show()

alertDialogBuilder 
      .setMessage("Click yes to exit!") 
      .setCancelable(false) 
      .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, close 
        // current activity 
        MainActivity.this.finish(); 
       } 
       }) 
      .setNegativeButton("No",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, just close 
        // the dialog box and do nothing 
        dialog.cancel(); 
       } 
      }).show().getWindow().setLayout(600,500); 
8

試圖調整大小後的佈局,首先檢查你的對話框是使用什麼樣的風格之前。確保沒有在樣式樹集合

<item name="windowMinWidthMajor">...</item> 
<item name="windowMinWidthMinor">...</item> 

如果這是發生,它只是作爲提供自己的風格,以[建設者構造函數在themeResId(http://developer.android.com/reference/android/app/AlertDialog.Builder.html#AlertDialog.Builder(android.content.Context,INT))可用的API一樣簡單11+

<style name="WrapEverythingDialog" parent=[whatever you were previously using]> 
    <item name="windowMinWidthMajor">0dp</item> 
    <item name="windowMinWidthMinor">0dp</item> 
</style> 
0

如果要基於設備框架添加動態寬度和高度,可以執行這些計算並指定高度和寬度。

AlertDialog.Builder builderSingle = new 
AlertDialog.Builder(getActivity()); 
builderSingle.setTitle("Title"); 

final AlertDialog alertDialog = builderSingle.create(); 
alertDialog.show(); 

Rect displayRectangle = new Rect(); 
Window window = getActivity().getWindow(); 

window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle); 

alertDialog.getWindow().setLayout((int)(displayRectangle.width() * 
0.8f), (int)(displayRectangle.height() * 0.8f)); 

P.S:顯示對話框,然後再嘗試修改窗口的佈局屬性

相關問題