Android Dialog宽度设置固定大小/比例
获取屏幕大小设置固定宽度或者固定比例
public class DialogUtils {
public static void setDialog(Dialog dialog) {
Display display = dialog.getWindow().getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = (int)(display.getWidth() * 0.9);
dialog.getWindow().setAttributes(params);
}
}
使用时放在show()方法后面确保起作用
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("退出登录")
.setMessage("确定退出吗?")
.setPositiveButton("确定", (dialogInterface, i) -> {})
.setNegativeButton("取消", (dialogInterface, i) -> {})
.create();
dialog.show();
DialogUtils.setDialog(dialog);