看了一个增加弹框优先级的文字,自己写了一个自定义弹框,加了优先级,效果还是不错的
首先是自定义弹框MyBaseDialog(可导入布局,也有默认布局) , 实现的MyDialogInterface接口就是为了优先级的:
public class MyBaseDialog extends Dialog implements MyDialogInterface {
private String title;
private String content;
/**
* true 显示2个按钮
* false 显示1个按钮
*/
private boolean showTwoBtn;
private String leftStr;
private View.OnClickListener leftListener;
private String rightStr;
private View.OnClickListener rightListener;
private boolean cancelable;
private boolean canceledOnTouchOutside;
/**
* 是否使用自定义弹框
* useUseDefineView = true,需要传入useDefineLayout
*/
private boolean useUseDefineView;
private View useDefineLayout;
/**
* 是否被挤出(每个实现DialogManager.Dialog的窗口类都需要新建该变量)
*/
private boolean isCrowdOut;
private MyBaseDialog(Context context, Builder builder) {
super(context);
useUseDefineView = builder.useUseDefineView;
useDefineLayout = builder.useDefineLayout;
cancelable = builder.cancelable;
canceledOnTouchOutside = builder.canceledOnTouchOutside;
if (useUseDefineView) {
setContentView(useDefineLayout);
} else {
title = builder.title;
content = builder.content;
showTwoBtn = builder.showTwoBtn;
leftStr = builder.leftStr;
leftListener = builder.leftListener;
rightStr = builder.rightStr;
rightListener = builder.rightListener;
View view = LayoutInflater.from(context).inflate(R.layout.dialog_my_base_dialog_layout, null);
setContentView(view);
TextView tvTitle = view.findViewById(R.id.tv_dialog_title);
TextView tvContent = view.findViewById(R.id.tv_dialog_content);
TextView tvLeftBtn = view.findViewById(R.id.tv_dialog_btn_left);
View line = view.findViewById(R.id.view_dialog_btn_line);
TextView tvRightBtn = view.findViewById(R.id.tv_dialog_btn_right);
tvTitle.setText(title);
tvContent.setText(content);
tvLeftBtn.setText(leftStr);
tvLeftBtn.setOnClickListener(leftListener);
if (showTwoBtn) {
line.setVisibility(View.VISIBLE);
tvRightBtn.setVisibility(View.VISIBLE);
tvRightBtn.setText(rightStr);
tvRightBtn.setOnClickListener(rightListener);
} else {
line.setVisibility(View.GONE);
tvRightBtn.setVisibility(View.GONE);
}
}
setCancelable(cancelable);
setCanceledOnTouchOutside(canceledOnTouchOutside);
WindowManager.LayoutParams params = getWindow().getAttributes();
//getMetricsWidth(context) 得到屏幕宽度
params.width = Utils.getMetricsWidth(context) / 3 * 2;
//设置dialog宽度,高度相同处理即可 需在dialog.show()或者setContentView(view)后设置方有效果
getWindow().setAttributes(params);
//设置背景 很重要 不设不行~~~
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(Color.parseColor("#00000000"));
getWindow().setBackgroundDrawable(drawable);
}
@Override
public void dismiss(boolean isCrowdOut) {
//isCrowdOut在super.dismiss()之前赋值
this.isCrowdOut = isCrowdOut;
super.dismiss();
}
@Override
public void setOnDismissListener(final InterfaceManager.OnDismissListener listener) {
setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
listener.onDismiss(isCrowdOut);
}
});
}
@Override
public void setOnShowListener(final InterfaceManager.OnShowListener listener) {