android中設定AlertDialog的大小 .
阿新 • • 發佈:2019-01-31
AlertDialog dialog = builder.setTitle("訊息列表")
.setView(layout)
.create();
dialog.show();
//設定視窗的大小
dialog.getWindow().setLayout(300, 200);
dialog.show();一定要放在dialog.getWindow().setLayout(300, 200);的前面,否則不起作用。
網上有一種方法是WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.width = 300; params.height = 200; dialog.getWindow().setAttributes(params);
但是dialog.getWindow().setLayout(300, 200);實際上封裝了這個方法,setLayout()的原始碼如下:
final WindowManager.LayoutParams attrs = getAttributes();
attrs.width = width;
attrs.height = height;
if (mCallback != null) {
mCallback.onWindowAttributesChanged(attrs);
}
所以這兩個方法的作用本質上是一樣的,都是為AlertDialog設定大小