AlertDialog 宽度充满整个屏幕
时间: 2023-08-06 07:03:26 浏览: 89
您可以通过设置AlertDialog的window属性来实现宽度充满整个屏幕。具体可以按照以下步骤进行:
1. 创建AlertDialog实例
```
AlertDialog.Builder builder = new AlertDialog.Builder(context);
```
2. 获取AlertDialog的window对象
```
AlertDialog dialog = builder.create();
Window window = dialog.getWindow();
```
3. 设置window的属性
```
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
```
其中,第一个参数设置宽度为屏幕的宽度,第二个参数设置高度为自适应。
完整代码示例:
```
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定要删除吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 删除操作
}
});
builder.setNegativeButton("取消", null);
AlertDialog dialog = builder.create();
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
dialog.show();
```
阅读全文