(基本都是用这个主题)无边框全透明背景:
res/values/styles:
--自定义dialog背景全透明无边框theme -->
比如:title+message+确定按钮、取消按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:gravity="center"
android:text="消息提示"
android:textColor="#38ADFF"
android:textSize="16sp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="提示消息" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="No"
android:textColor="#7D7D7D"
android:textSize="16sp" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="Yes"
android:textColor="#38ADFF"
android:textSize="16sp" />
LinearLayout>
LinearLayout>
RelativeLayout>
dialog_bg.xml如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:radius="6dp" />
shape>
public class CustomDialog extends Dialog {
private Button yes;
private Button no;
private TextView titleTv;
private TextView messageTv;
private String titleStr;
private String messageStr;
private String yesStr, noStr;
/* -------------------------------- 接口监听 ------------------------------------- */
private onNoOnclickListener noOnclickListener;
private onYesOnclickListener yesOnclickListener;
public interface onYesOnclickListener {
void onYesClick();
}
public interface onNoOnclickListener {
void onNoClick();
}
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = onYesOnclickListener;
}
/* ---------------------------------- 构造方法 ------------------------------------- */
public CustomDialog(Context context) {
super(context, R.style.MyDialog);//风格主题
}
/* ---------------------------------- onCreate------------------------------------- */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_layout);//自定义布局
//按空白处不能取消动画
// setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面数据
initData();
//初始化界面控件的事件
initEvent();
}
/**
* 初始化界面的确定和取消监听器
*/
private void initEvent() {
//设置确定按钮被点击后,向外界提供监听
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesClick();
}
}
});
//设置取消按钮被点击后,向外界提供监听
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
}
/**
* 初始化界面控件的显示数据
*/
private void initData() {
//如果用户自定了title和message
if (titleStr != null) {
titleTv.setText(titleStr);
}
if (messageStr != null) {
messageTv.setText(messageStr);
}
//如果设置按钮的文字
if (yesStr != null) {
yes.setText(yesStr);
}
if (noStr != null) {
no.setText(noStr);
}
}
/**
* 初始化界面控件
*/
private void initView() {
yes = (Button) findViewById(R.id.yes);
no = (Button) findViewById(R.id.no);
titleTv = (TextView) findViewById(R.id.title);
messageTv = (TextView) findViewById(R.id.message);
}
/* ---------------------------------- set方法 传值------------------------------------- */
//为外界设置一些public 公开的方法,来向自定义的dialog传递值
public void setTitle(String title) {
titleStr = title;
}
public void setMessage(String message) {
messageStr = message;
}
}
final CustomDialog customDialog = new CustomDialog(MainActivityI.this);
customDialog.setTitle("提示");
customDialog.setMessage("确定退出应用?");
customDialog.show();
customDialog.setYesOnclickListener("确定", new CustomDialog.onYesOnclickListener() {
@Override
public void onYesClick() {
Toast.makeText(MainActivityI.this, "点击了--确定--按钮", Toast.LENGTH_LONG).show();
customDialog.dismiss();
}
});
customDialog.setNoOnclickListener("取消", new CustomDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
Toast.makeText(MainActivityI.this, "点击了--取消--按钮", Toast.LENGTH_LONG).show();
customDialog.dismiss();
}
});
效果如图:
参考:自定义Dialog的简单实现
1、设置标题和内容可以使用构造方法
2、监听确定和取消事件可以使用一个接口:
如下
注意最外层的宽度为match_parent,方便后面修改宽度
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:gravity="center"
android:text="消息提示"
android:textColor="#38ADFF"
android:textSize="16sp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="提示消息" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="No"
android:textColor="#7D7D7D"
android:textSize="16sp" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="Yes"
android:textColor="#38ADFF"
android:textSize="16sp" />
LinearLayout>
LinearLayout>
public class MyDialog extends Dialog {
private Context context;
private String message;
private String title;
private String confirmButtonText;
private String cacelButtonText;
/* ----------------------------------接口监听---------------------------------------- */
private ClickListenerInterface clickListenerInterface;
public interface ClickListenerInterface {
void doConfirm();
void doCancel();
}
public void setClickListener(ClickListenerInterface clickListenerInterface) {
this.clickListenerInterface = clickListenerInterface;
}
/* ----------------------------------构造方法:传值------------------------------------- */
public MyDialog(Context context, String title, String message, String confirmButtonText, String cacelButtonText) {
super(context, R.style.MyDialog);
this.context = context;
this.title = title;
this.message = message;
this.confirmButtonText = confirmButtonText;
this.cacelButtonText = cacelButtonText;
}
/* ---------------------------------- onCreate ------------------------------------- */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
@SuppressWarnings("ConstantConditions")
@SuppressLint("InflateParams")
public void init() {
setContentView(R.layout.my_dialog);
TextView tvTitle = (TextView) findViewById(R.id.title);
TextView tvMessage = (TextView) findViewById(R.id.message);
TextView tvConfirm = (TextView) findViewById(R.id.yes);
TextView tvCancel = (TextView) findViewById(R.id.no);
tvTitle.setText(title);
tvMessage.setText(message);
tvConfirm.setText(confirmButtonText);
tvCancel.setText(cacelButtonText);
tvConfirm.setOnClickListener(new clickListener());
tvCancel.setOnClickListener(new clickListener());
setCanceledOnTouchOutside(false);//设置窗口外是否可关闭
setOnKeyListener(new DialogInterface.OnKeyListener() {//设置按back键不关闭
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_BACK;
}
});
Window dialogWindow = getWindow();
if (dialogWindow != null) {
dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER);//设置窗口位置
dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口进出动画
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
if (lp != null) {
lp.width = (int) (d.widthPixels * 0.7); // 设置为屏幕宽度
}
dialogWindow.setAttributes(lp);
}
}
private class clickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.yes:
clickListenerInterface.doConfirm();
break;
case R.id.no:
clickListenerInterface.doCancel();
break;
}
}
}
}
final MyDialog myDialog = new MyDialog(MainActivityI.this, "提示", "确定要退出吗?", "退出", "取消");
myDialog.show();
myDialog.setClickListener(new MyDialog.ClickListenerInterface() {
@Override
public void doConfirm() {
Toast.makeText(MainActivityI.this, "点击了--确定--按钮", Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
@Override
public void doCancel() {
Toast.makeText(MainActivityI.this, "点击了--取消--按钮", Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
});
res/values/styles:
<style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/animator_dialog_in
- "android:windowExitAnimation"
>@anim/animator_dialog_out
style>
animator_dialog_in
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:fromXDelta="100%"
android:toXDelta="0" />
set>
animator_dialog_out
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:fromXDelta="0"
android:toXDelta="-100%" />
set>
效果如图:
参考:Android自定义Dialog
如果对话框需要添加动画,推荐使用dialogFragment,自定义dialog添加动画,在按home键后会自动执行动画。监听back键相同。