您的位置:首页 > 移动开发 > Android开发

android中设置AlertDialog的大小 .

2014-04-02 17:39 190 查看


[html]
view plaincopyprint?

AlertDialog dialog = builder.setTitle("消息列表") .setView(layout) .create(); dialog.show(); //设置窗口的大小 dialog.getWindow().setLayout(300, 200);
AlertDialog dialog = builder.setTitle("消息列表")
.setView(layout)
.create();
dialog.show();
//设置窗口的大小
dialog.getWindow().setLayout(300, 200);


dialog.show();一定要放在dialog.getWindow().setLayout(300, 200);的前面,否则不起作用。
网上有一种方法是

[html]
view plaincopyprint?

WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.width = 300; params.height = 200; dialog.getWindow().setAttributes(params);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = 300;
params.height = 200;
dialog.getWindow().setAttributes(params);


但是dialog.getWindow().setLayout(300, 200);实际上封装了这个方法,setLayout()的源代码如下:

[html]
view plaincopyprint?

final WindowManager.LayoutParams attrs = getAttributes(); attrs.width = width; attrs.height = height; if (mCallback != null) { mCallback.onWindowAttributesChanged(attrs); }
final WindowManager.LayoutParams attrs = getAttributes();
attrs.width = width;
attrs.height = height;
if (mCallback != null) {
mCallback.onWindowAttributesChanged(attrs);
}


所以这两个方法的作用本质上是一样的,都是为AlertDialog设置大小
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: