android - 如何控制Android中默认Alert Dialog的宽度和高度?

标签 android android-widget

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.

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

最佳答案

Sat Code 仅稍作改动,在 AlertDialogshow() 方法后设置布局。

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

关于android - 如何控制Android中默认Alert Dialog的宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4406804/

相关文章:

android - 如何复制消息应用程序中选择联系人的功能?

android - 以编程方式用按钮填充屏幕

android - RTL AlertDialog 中的负数

android - float Action 按钮异常Android

android - 左侧为 Listview,右侧为 tabhost,带有 20 :80 screen sharing ratio

android - RemoteViews setOnClickPendingIntent 未针对 Widget ListView 触发

android - StackView Widget 示例源代码

android - android 中的多部分请求问题 - 抛出 BAD REQUEST

android - 如何以及在何处使用 Fragments 中的 setInitialSavedState 和 saveFragmentInstanceState

android - 在 PopupMenu 显示时旋转,获取 android.view.WindowLeaked