样式 放在style下面
<!--<style name="DialogStyle" parent="@android:style/Theme.Dialog">-->
<style name="DialogStyle" parent="@android:style/Theme.Dialog">
<!--<item name="android:windowAnimationStyle">@style/AnimBottom</item>-->
<!--<item name="android:windowFrame">@null</item>-->
<!--<!– 边框 –>-->
<!--<item name="android:windowIsFloating">false</item>-->
<!--<!– 是否浮现在activity之上 –>-->
<!--<item name="android:windowIsTranslucent">true</item>-->
<!--<!– 半透明 –>-->
<!--<item name="android:windowNoTitle">true</item>-->
<!--<!– 无标题 –>-->
<!--<item name="android:windowBackground">@android:color/transparent</item>-->
<!--<!– 背景透明 –>-->
<!--<item name="android:backgroundDimEnabled">true</item>-->
<!--<!– 模糊 –>-->
<!-- 去黑边 -->
<item name="android:windowFrame">@null</item>
<!-- 设置是否可滑动 -->
<item name="android:windowIsFloating">false</item>
<!-- 设置是否透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 背景 -->
<item name="android:background">@null</item>
<!-- 窗口背景 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 点击空白部分activity不消失 -->
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
UpdateDialog.java java类
package .widgets;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import .R;
/**
* Created by Kane_lkh on 2016/3/10.
*/
public class UpdateDialog extends Dialog {
public UpdateDialog(Context context, int theme) {
super(context, theme);
}
public UpdateDialog(Context context) {
super(context);
}
/**
* Helper class for creating a custom dialog
*/
public static class Builder{
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private OnClickListener positiveButtonClickListener, negativeButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* 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 title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set a custom content view for the Dialog.
* If a message is set, the contentView is not
* added to the Dialog...
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
this.positiveButtonText = (String) context.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button resource and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
this.negativeButtonText = (String) context.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*/
public UpdateDialog create() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final UpdateDialog dialog = new UpdateDialog(context, R.style.DialogStyle);
dialog.setCanceledOnTouchOutside(false);
View layout = inflater.inflate(R.layout.update_dialog_layout, null);
dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(.title)).setText(title);
// set the confirm button
if (positiveButtonText != null) {
((Button) layout.findViewById(.positiveButton)).setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(.positiveButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(.positiveButton).setVisibility(View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
((Button) layout.findViewById(.negativeButton)).setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(.negativeButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(.negativeButton).setVisibility(View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(.message)).setText(message);
} else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(.content)).removeAllViews();
((LinearLayout) layout.findViewById(.content)).addView(contentView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
update_dialog_layout.xml 布局文件
<?xml version="1.0" encoding="UTF-8"?>
-<LinearLayout android:background="@null" android:paddingRight="@dimen/dimen_20" android:paddingLeft="@dimen/dimen_20" android:paddingTop="@dimen/dimen_140" android:orientation="vertical" android:minWidth="280dip" android:layout_height="match_parent" android:layout_width="wrap_content" xmlns:tools="http:///tools" xmlns:android="http:///apk/res/android">
-<LinearLayout android:background="@drawable/bg_title_update_dialog" android:paddingTop="10dip" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:paddingBottom="10dip">
<ImageView android:layout_height="32dp" android:layout_width="32dp" android:src="@drawable/ic_launcherx" android:layout_marginLeft="10dip" android:layout_gravity="center_vertical"/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="8dip" android:layout_gravity="center_vertical" android:textSize="@dimen/text_size_16" android:id="@+id/title"/>
</LinearLayout>
-<LinearLayout android:background="@drawable/bg_middle_update_dialog" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/content" android:gravity="center" android:minHeight="100dip">
<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginLeft="10dp" android:textSize="16sp" android:id="@+id/message" android:textColor="#FF000000" android:layout_marginRight="10dp"/>
</LinearLayout>
-<LinearLayout android:background="@drawable/bg_bottom_update_dialog" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:padding="5dip">
<Button android:background="@drawable/bbuton_info_rounded" android:layout_height="40dp" android:layout_width="0dip" android:textSize="@dimen/text_size_18" android:id="@+id/positiveButton" android:textColor="@color/bottom_text_color_normal" android:singleLine="true" android:layout_weight="1"/>
<Button android:background="@drawable/bbuton_danger_rounded" android:layout_height="@dimen/dimen_40" android:layout_width="0dip" android:layout_marginLeft="3dip" android:textSize="@dimen/text_size_18" android:id="@+id/negativeButton" android:textColor="@color/bottom_text_color_normal" android:singleLine="true" android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
bg_title_update_dialog.xml 背景
<?xml version="1.0" encoding="UTF-8"?>
-<selector xmlns:android="http:///apk/res/android">
-<item>
-<shape>
<gradient android:startColor="#e1e1e1" android:endColor="#e1e1e1" android:angle="270"/>
<corners android:topRightRadius="15dp" android:topLeftRadius="15dp"/>
</shape>
</item>
</selector>