一、定义Dialog类
public class NoticeDialog extends Dialog {
private Context context;
private int height, width;
private boolean cancelTouchout;
private View view;
public NoticeDialog(Builder builder) {
super(builder.context);
context = builder.context;
height = builder.height;
width = builder.width;
cancelTouchout = builder.cancelTouchout;
view = builder.view;
}
private NoticeDialog(Builder builder, int resStyle) {
super(builder.context, resStyle);
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);//调整dialog大小
}
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 heightpx(int val) {
height = val;
return this;
}
//设置宽度
public Builder widthpx(int val) {
width = val;
return this;
}
//设置风格
public Builder style(int resStyle) {
this.resStyle = resStyle;
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 Builder addViewVisible(int visibility){
view.findViewById(R.id.dialogCancelButton).setVisibility(visibility);
return this;
}
//imageView添加图片
public Builder addViewImageImage(int imageId){
view.findViewById(R.id.dialogImageView).setBackgroundResource(imageId);
return this;
}
//imageButton添加图片
public Builder addButtonImage(int imageId){
view.findViewById(R.id.dialogCancelButton).setBackgroundResource(imageId);
return this;
}
public NoticeDialog build() {
if (resStyle != -1) {
return new NoticeDialog(this, resStyle);
} else {
return new NoticeDialog(this);
}
}
}
}
二、自定义Dialog的使用
private NoticeDialog noConnectdialog;//定义自定义dialog
NoticeDialog.Builder builder = new NoticeDialog.Builder(FunSelectActivity.this);
noConnectdialog = builder.cancelTouchout(false)
.view(R.layout.activity_notice_dialog)
.widthpx(500)
.heightpx(500)
.style(R.style.my_dialog)
.addViewImageImage(R.mipmap.notice_not_connect)
.addButtonImage(R.mipmap.notice_ok)
.addViewOnclick(R.id.dialogCancelButton,new View.OnClickListener() {
@Override
public void onClick(View view) {
noConnectdialog.dismiss();
musicEffects.play_music("BUTTON_PRESS");
}
}).build();
//为自定义Dialog添加动画
Window window = noConnectdialog.getWindow();
window.setWindowAnimations(R.style.style_dialog);
noConnectdialog.show();