2010-12-10 199 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:显示对话框,然后再尝试修改窗口的布局属性

相关问题