//代码
AlertDialog alertDialog;
// 创建构建器
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// 设置参数
builder.setTitle("提示")
.setNeutralButton("忽略", new DialogInterface.OnClickListener() {// 中那个按钮
@Override
public void onClick(DialogInterface dialog,int which) {
// dismissDialog();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// dismissDialog();
}
});
alertDialog= builder.create();
WindowManager.LayoutParams params = alertDialog.getWindow()
.getAttributes();
alertDialog.getWindow().setAttributes(params);
alertDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
alertDialog.show();
try {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
}
dialog.show();
} catch (Exception e) {
e.printStackTrace();
}
参考博客https://blog.csdn.net/weixin_39170886/article/details/103857336,感谢