1.在res/value/styles下
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
2.复制粘贴下面的
public class MyDialog extends Dialog {
private Context context;
private int height, width;
private boolean cancelTouchout;
private View view;
public MyDialog(@NonNull Builder builder) {
super(builder.context);
context = builder.context;
context = builder.context;
height = builder.height;
width = builder.width;
cancelTouchout = builder.cancelTouchout;
view = builder.view;
}
public MyDialog(@NonNull Builder builder, @StyleRes int themeResId) {
super(builder.context, themeResId);
context = builder.context;
height = builder.height;
width = builder.width;
cancelTouchout = builder.cancelTouchout;
view = builder.view;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
setCanceledOnTouchOutside(cancelTouchout);
Window win = getWindow();
WindowManager.LayoutParams lp = win.getAttributes();
lp.gravity = Gravity.CENTER;
lp.height = height;
lp.width = width;
win.setAttributes(lp);
}
public static final class Builder {
private Context context;
private int height, width;
private boolean cancelTouchout;
private View view;
private int resStyle = -1;
public Builder(Context context) {
this.context = context;
}
public Builder view(int resView) {
view = LayoutInflater.from(context).inflate(resView, null);
return this;
}
public Builder style(int resStyle) {
this.resStyle = resStyle;
return this;
}
public Builder heightdp(int val) {
height = DensityUtil.dip2px(context, val);
return this;
}
public Builder widthdp(int val) {
width = DensityUtil.dip2px(context, val);
return this;
}
public Builder cancelTouchout(boolean val) {
cancelTouchout = val;
return this;
}
public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
view.findViewById(viewRes).setOnClickListener(listener);
return this;
}
public MyDialog build() {
return resStyle == -1 ? new MyDialog(this) : new MyDialog(this, resStyle);
}
}
}
用法
MyDialog.Builder builder = new MyDialog.Builder(WaitPayActivity.this);
dialog = builder.style(R.style.Dialog)
.view(R.layout.dialog_cancel_pay)
.heightdp(264)
.widthdp(240)
.cancelTouchout(false)
.addViewOnclick(R.id.btn_confirm, WaitPayActivity.this)
.build();
dialog.show();