R.layout.dialog
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff">
android:id="@+id/dialog_header"
android:orientation="vertical"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
android:background="@color/colorGreen"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
android:contentDescription="@id/dialog_title"
android:id="@+id/dialog_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_check_circle" />
android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:visibility="gone" />
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@+id/dialog_header"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
android:id="@+id/dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
tools:text="Dialog Message" />
InfoDialog.java
public class InfoDialog extends Dialog {
private InfoDialog(Context context, int themeResId) {
super(context, themeResId);
}
public static class Builder {
private View mLayout;
private ImageView mIcon;
private TextView mTitle;
private TextView mMessage;
private Button mButton;
private View.OnClickListener mButtonClickListener;
private InfoDialog mDialog;
public Builder(Context context) {
mDialog = new InfoDialog(context, R.style.Theme_AppCompat_Dialog);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
mLayout = inflater.inflate(R.layout.dialog, null, false);
//添加布局文件到 Dialog
mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mIcon = mLayout.findViewById(R.id.dialog_icon);
mTitle = mLayout.findViewById(R.id.dialog_title);
mMessage = mLayout.findViewById(R.id.dialog_message);
mButton = mLayout.findViewById(R.id.dialog_button);
}
/**
* 通过 ID 设置 Dialog 图标
*/
public Builder setIcon(int resId) {
mIcon.setImageResource(resId);
return this;
}
/**
* 用 Bitmap 作为 Dialog 图标
*/
public Builder setIcon(Bitmap bitmap) {
mIcon.setImageBitmap(bitmap);
return this;
}
/**
* 设置 Dialog 标题
*/
public Builder setTitle(@NonNull String title) {
mTitle.setText(title);
mTitle.setVisibility(View.VISIBLE);
return this;
}
/**
* 设置 Message
*/
public Builder setMessage(@NonNull String message) {
mMessage.setText(message);
return this;
}
/**
* 设置按钮文字和监听
*/
public Builder setButton(@NonNull String text, View.OnClickListener listener) {
mButton.setText(text);
mButtonClickListener = listener;
return this;
}
public InfoDialog create() {
mButton.setOnClickListener(view -> {
mDialog.dismiss();
mButtonClickListener.onClick(view);
});
mDialog.setContentView(mLayout);
mDialog.setCancelable(true); //用户可以点击后退键关闭 Dialog
mDialog.setCanceledOnTouchOutside(false); //用户不可以点击外部来关闭 Dialog
return mDialog;
}
}
}
调用方法
InfoDialog infoDialog = new InfoDialog.Builder(this)
.setTitle("Done")
.setMessage("Something done")
.setButton("OK", view ->
Toast.makeText(this, "OK Clicked.", Toast.LENGTH_SHORT).show()
).create();
infoDialog.show();
});