前言:在做項目的時候,發現dialog界面太醜陋,從csdn上下載下傳了一份自定義dialog的源碼,在他的基礎上對界面進行美化...有需要的朋友可以直接拿走
效果圖如下:
主要代碼:
/**
* 自定義dialog
* @author ansen
*/
public class customdialog extends dialog {
public customdialog(context context) {
super(context);
}
public customdialog(context context, int theme) {
super(context, theme);
public static class builder {
private context context;
private string title;
private string message;
private string positivebuttontext;
private string negativebuttontext;
private view contentview;
private dialoginterface.onclicklistener positivebuttonclicklistener;
private dialoginterface.onclicklistener negativebuttonclicklistener;
public builder(context context) {
this.context = context;
}
public builder setmessage(string message) {
this.message = message;
return this;
/**
* set the dialog message from resource
* @param title
* @return
*/
public builder setmessage(int message) {
this.message = (string) context.gettext(message);
* set the dialog title from resource
public builder settitle(int title) {
this.title = (string) context.gettext(title);
* set the dialog title from string
public builder settitle(string title) {
this.title = title;
public builder setcontentview(view v) {
this.contentview = v;
* set the positive button resource and it's listener
* @param positivebuttontext
public builder setpositivebutton(int positivebuttontext,
dialoginterface.onclicklistener listener) {
this.positivebuttontext = (string) context
.gettext(positivebuttontext);
this.positivebuttonclicklistener = listener;
public builder setpositivebutton(string positivebuttontext,
this.positivebuttontext = positivebuttontext;
public builder setnegativebutton(int negativebuttontext,
this.negativebuttontext = (string) context
.gettext(negativebuttontext);
this.negativebuttonclicklistener = listener;
public builder setnegativebutton(string negativebuttontext,
this.negativebuttontext = negativebuttontext;
//建立dialog對象 主要就是這個方法,加載自定義布局檔案
public customdialog create() {
layoutinflater inflater = (layoutinflater) context
.getsystemservice(context.layout_inflater_service);
// instantiate the dialog with the custom theme
final customdialog dialog = new customdialog(context,
r.style.dialog);
view layout = inflater.inflate(r.layout.dialog_normal_layout, null);
dialog.addcontentview(layout, new layoutparams(
layoutparams.fill_parent, layoutparams.wrap_content));
// set the dialog title
((textview) layout.findviewbyid(r.id.title)).settext(title);
// set the confirm button
if (positivebuttontext != null) {
((button) layout.findviewbyid(r.id.positivebutton))
.settext(positivebuttontext);
if (positivebuttonclicklistener != null) {
((button) layout.findviewbyid(r.id.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(r.id.positivebutton).setvisibility(
view.gone);
}
// set the cancel button
if (negativebuttontext != null) {
((button) layout.findviewbyid(r.id.negativebutton))
.settext(negativebuttontext);
if (negativebuttonclicklistener != null) {
((button) layout.findviewbyid(r.id.negativebutton))
negativebuttonclicklistener.onclick(dialog,
dialoginterface.button_negative);
layout.findviewbyid(r.id.negativebutton).setvisibility(
// set the content message
if (message != null) {
((textview) layout.findviewbyid(r.id.message)).settext(message);
} else if (contentview != null) {
((linearlayout) layout.findviewbyid(r.id.content))
.removeallviews();
((linearlayout) layout.findviewbyid(r.id.content)).addview(
contentview, new layoutparams(layoutparams.fill_parent,
layoutparams.fill_parent));
dialog.setcontentview(layout);
return dialog;
}
4.activity中如何調用:
customdialog.builder builder = new customdialog.builder(this);
builder.setmessage("這個就是自定義的提示框");
builder.settitle("提示");
builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
public void onclick(dialoginterface dialog, int which) {
dialog.dismiss();
//設定你的操作事項
});
builder.setnegativebutton("取消",
new android.content.dialoginterface.onclicklistener() {
public void onclick(dialoginterface dialog, int which) {
dialog.dismiss();
});
builder.create().show();