如何控制Android中默认警报对话框的宽度和高度?

问题描述 投票:61回答:9
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。 如何控制默认警报对话框的宽度和高度? 我试过了:

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

如何在警报窗口中获取布局参数并手动设置宽度和高度?

android android-widget
9个回答
180
投票

只有Sat Code的微小变化,在show()AlertDialog方法之后设置布局。

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);

0
投票
longButton.setOnClickListener {
  show(
    "1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" +
      "1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" +
      "1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" +
      "1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" +
      "1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" +
      "1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" +
      "1234567890-12345678901234567890123456789012345678901234567890"
  )
}

shortButton.setOnClickListener {
  show(
    "1234567890\n" +
      "1234567890-12345678901234567890123456789012345678901234567890"
  )
}

private fun show(msg: String) {
  val builder = AlertDialog.Builder(this).apply {
    setPositiveButton(android.R.string.ok, null)
    setNegativeButton(android.R.string.cancel, null)
  }

  val dialog = builder.create().apply {
    setMessage(msg)
  }
  dialog.show()

  dialog.window?.decorView?.addOnLayoutChangeListener { v, _, _, _, _, _, _, _, _ ->
    val displayRectangle = Rect()
    val window = dialog.window
    v.getWindowVisibleDisplayFrame(displayRectangle)
    val maxHeight = displayRectangle.height() * 0.6f // 60%

    if (v.height > maxHeight) {
      window?.setLayout(window.attributes.width, maxHeight.toInt())
    }
  }
}

short message

long message


26
投票

好的,我可以使用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();

13
投票

在尝试调整布局后的大小之前,首先要检查对话框使用的样式。确保样式树中没有任何内容设置

<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>

9
投票

这也适用于在.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);

5
投票

如果要根据设备框架添加动态宽度和高度,可以执行这些计算并指定高度和宽度。

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


4
投票

我不知道你是否可以改变AlertDialog的默认高度/宽度,但如果你想这样做,我认为你可以通过创建自己的自定义对话框来实现。您只需在android manifest.xml中为您的活动提供android:theme="@android:style/Theme.Dialog",并可根据您的要求编写整个布局。您可以从Android Resource XML设置自定义对话框的高度和宽度。


3
投票

对于那些会发现像我这样的线程的人来说,这里是IMO更好的解决方案(注意70%设置对话框的最小宽度,以屏幕宽度的百分比表示):

<style name="my_dialog" parent="Theme.AppCompat.Dialog.Alert">
    <item name="windowMinWidthMajor">70%</item>
    <item name="windowMinWidthMinor">70%</item>
</style>

然后将此样式应用于对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.my_dialog);

1
投票

欣赏由Sid回答,因为它的动态,但我想添加一些东西。

如果您只想更改宽度,高度将是原样。

我做了如下:

    // All process of AlertDialog
    AlertDialog alert = builder.create();
    alert.show();

    // Creating Dynamic
    Rect displayRectangle = new Rect();

    Window window = getActivity().getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
    alert.getWindow().setLayout((int) (displayRectangle.width() *
            0.8f), alert.getWindow().getAttributes().height);

在这里,我使用alert.getWindow().getAttributes().height保持高度,因为它是AlertDialogWidth将根据屏幕分辨率更改。

希望它会有所帮助。谢谢。


0
投票

我认为tir38的答案是最干净的解决方案。请记住,如果您使用的是android.app.AlertDialog和holo主题,那么您的样式应该如下所示

<style name="WrapEverythingDialog" parent=[holo theme ex. android:Theme.Holo.Dialog]>
    <item name="windowMinWidthMajor">0dp</item>
    <item name="windowMinWidthMinor">0dp</item>
</style>

如果在AppCompat主题中使用support.v7.app.AlertDialog或androidx.appcompat.app.AlertDialog,您的样式应如下所示

<style name="WrapEverythingDialog" parent=[Appcompat theme ex. Theme.AppCompat.Light.Dialog.Alert]>
    <item name="android:windowMinWidthMajor">0dp</item>
    <item name="android:windowMinWidthMinor">0dp</item>
</style>
© www.soinside.com 2019 - 2024. All rights reserved.