public class ConfirmCustomDialog extends Dialog {
public ConfirmCustomDialog(Context context, int theme) {
super(context, theme);
}
public ConfirmCustomDialog(Context context) {
super(context);
}
@Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
int uiOptions =View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
window.getDecorView().setSystemUiVisibility(uiOptions);
getWindow().setStatusBarColor(Color.TRANSPARENT);
getWindow().setNavigationBarColor(Color.TRANSPARENT);
}
}
/**
* Helper class for creating a custom dialog
*/
public static class Builder {
private Context context;
private int iconId;
private int resId;
private String title;
private String message;
private DialogInterface.OnClickListener positiveButtonClickListener;
private DialogInterface.OnClickListener negativeButtonClickListener;
public Builder(Context context) {
this.context = context;
}
public Builder setIcon(int iconId) {
this.iconId = iconId;
return this;
}
public Builder setPositiveButtonBackground(int resId) {
this.resId = resId;
return this;
}
/**
* Set the Dialog message from String
*
* @param
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
* @param
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
* @param
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
*
* @param
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set the positive button text and it's listener
*
* @param listener
* @return
*/
public Builder setPositiveButton(DialogInterface.OnClickListener listener) {
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
*
* @param listener
* @return
*/
public Builder setNegativeButton(DialogInterface.OnClickListener listener) {
this.negativeButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*/
public ConfirmCustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final ConfirmCustomDialog dialog = new ConfirmCustomDialog(context, R.style.Dialog);
dialog.setCanceledOnTouchOutside(false);
View layout = inflater.inflate(R.layout.layout_confirm_custom_dialog, null);
dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if(iconId > 0) {
((ImageView) layout.findViewById(R.id.title_icon)).setImageResource(iconId);
}
if(resId > 0) {
((ImageView) layout.findViewById(R.id.confirm_dialog_positive_button)).setImageResource(resId);
}
// set the dialog title
if (title != null) {
((TextView) layout.findViewById(R.id.confirm_dialog_title)).setText(title);
}
// set the confirm button
if (positiveButtonClickListener != null) {
((ImageView) layout.findViewById(R.id.confirm_dialog_positive_button))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
// set the cancel button
if (negativeButtonClickListener != null) {
((ImageView) layout.findViewById(R.id.confirm_dialog_negative_button))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}else {
((ImageView) layout.findViewById(R.id.confirm_dialog_negative_button)).setVisibility(View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.confirm_dialog_message)).setText(message);
}
dialog.setContentView(layout);
return dialog;
}
}
}
layout_confirm_custom_dialog.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/dp_210"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
android:orientation="vertical" >
<ImageView
android:layout_width="@dimen/dp_29"
android:layout_height="@dimen/dp_29"
android:layout_marginTop="@dimen/dp_12"
android:src="@mipmap/warning_icon"
android:layout_gravity="center"
android:id="@+id/title_icon"/>
<RelativeLayout
android:layout_width="@dimen/dp_180"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginRight="@dimen/dp_15">
<TextView
android:id="@+id/confirm_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="1"
android:textColor="#4D4D4D"
android:textSize="@dimen/sp_12"
android:layout_gravity="center" />
</RelativeLayout>
<RelativeLayout
android:layout_width="@dimen/dp_180"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_9"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginRight="@dimen/dp_15">
<TextView
android:id="@+id/confirm_dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="3"
android:textColor="#989898"
android:textSize="@dimen/sp_10"
android:layout_gravity="center" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_15"
android:layout_marginBottom="@dimen/dp_12"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginRight="@dimen/dp_15"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:layout_width="@dimen/dp_84"
android:layout_height="@dimen/dp_25"
android:src="@mipmap/cancel"
android:layout_marginRight="@dimen/dp_11"
android:id="@+id/confirm_dialog_negative_button" />
<ImageView
android:layout_width="@dimen/dp_84"
android:layout_height="@dimen/dp_25"
android:src="@mipmap/confirm"
android:id="@+id/confirm_dialog_positive_button" />
</LinearLayout>
</LinearLayout>
dialog样式:
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:backgroundDimEnabled">true</item>
</style>