gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:16:15 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com