如何控制Android中默认警报对话框的宽度和高度? [英] How to control the width and height of the default Alert Dialog in Android?

查看:87
本文介绍了如何控制Android中默认警报对话框的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用上面的代码来显示警报对话框.默认情况下,它将以宽度填充屏幕,以wrapper_content填充高度.
如何控制默认警报对话框的宽度和高度?
我试过了:

I am using the above code to display an Alert Dialog. By default, it fills the screen in width and wrap_content in height.
How can I control the width and height of default alert dialog ?
I tried:

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

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

How to get the layout params on the alert window and manually set the width and height?

推荐答案

仅对Sat代码稍作更改,请在AlertDialogshow()方法之后设置布局.

Only a slight change in Sat Code, set the layout after show() method of 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.

或者您可以按照我的方式来做.

Or you can do it in my way.

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中默认警报对话框的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆