自定义Dialog弹窗效果图:
CustomDialog.java
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class CustomDialog extends Dialog {
private Button yes; //确定按钮
private Button no; //取消按钮
private TextView title; //消息标题文本
private TextView message; //消息提示文本
private String titleStr; //从外界设置的title文本
private String messageStr; //从外界设置的消息文本
private String yesStr, noStr; //确定文本和取消文本的显示内容
private Window window = null;
private onYesOnClickListener yesOnClickListener; //确定按钮被点击了的监听器
private onNoClickListener noOnClickListener; //取消按钮被点击了的监听器
public CustomDialog(@NonNull Context context) {
super(context, R.style.CustomDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_layout);
//点击dialog以外的空白处是否隐藏
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面数据
initData();
//初始化界面控件的事件
initEvent();
//设置窗口显示
windowDeploy();
}
/**
* 初始化界面控件
*/
private void initView() {
yes = (Button) findViewById(R.id.yes);
no = (Button) findViewById(R.id.no);
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
}
/**
* 初始化界面控件的显示数据
*/
private void initData() {
if (TextUtils.isEmpty(titleStr)) {
title.setText(titleStr);
} else {
title.setText("应用提示");
}
if (messageStr != null) {
message.setText(messageStr);
}
if (yesStr != null) {
yes.setText(yesStr);
}
if (noStr != null) {
no.setText(noStr);
}
}
/**
* 初始化界面的确定和取消监听器
*/
private void initEvent() {
//设置确定按钮被点击后,向外界提供监听
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (yesOnClickListener != null) {
yesOnClickListener.onYesClick();
}
}
});
//设置取消按钮被点击后,向外界提供监听
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (noOnClickListener != null) {
noOnClickListener.onNoClick();
}
}
});
}
private void windowDeploy() {
window = getWindow();
window.setGravity(Gravity.CENTER); //设置窗口显示位置
window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
}
/**
* 设置确定按钮的显示内容和监听
*
* @param str
* @param onYesOnClickListener
*/
public void setYesOnClickListener(String str, onYesOnClickListener onYesOnClickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnClickListener = onYesOnClickListener;
}
/**
* 设置取消按钮的显示内容和监听
*
* @param str
* @param onNoClickListener
*/
public void setNoOnClickListener(String str, onNoClickListener onNoClickListener) {
if (str != null) {
noStr = str;
}
this.noOnClickListener = onNoClickListener;
}
/**
* 从外界Activity为Dialog设置标题
*
* @param title
*/
public void setTitle(String title) {
titleStr = title;
}
/**
* 从外界Activity为Dialog设置dialog的message
*
* @param message
*/
public void setMessage(String message) {
messageStr = message;
}
/**
* 设置确定按钮和取消被点击的接口
*/
public interface onYesOnClickListener {
void onYesClick();
}
public interface onNoClickListener {
void onNoClick();
}
}
custom_dialog_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/custom_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="@color/colorAccent"/>
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="@color/colorPrimary"
android:textSize="16sp"
android:gravity="center"
android:text="提示消息"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="@color/line" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="取消"
android:textColor="@color/font_color"
android:textSize="15sp"/>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@color/line" />
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:layout_gravity="center"
android:singleLine="true"
android:text="确认"
android:textColor="@color/orange"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
colors.xml
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="line">#e6e6e6</color>
<color name="font_color">#666666</color>
<color name="orange">#FF5500</color>
</resources>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!--自定义dialog背景全透明无边框theme -->
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
</style>
<style name="dialogWindowAnim" parent="android:Animation">
<!--Dialog弹出动画-->
<item name="android:windowEnterAnimation">@anim/push_up_in</item>
<!--Dialog隐藏动画-->
<item name="android:windowExitAnimation">@anim/push_up_out</item>
</style>
</resources>
push_up_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="300" />
</set>
push_up_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="300" />
</set>
ShowDialog.java
import android.content.Context;
public class ShowDialog {
private CustomDialog customDialog;
public ShowDialog() {
}
public void show(final Context context, String title, String message, final OnBottomClickListener onBottomClickListener) {
customDialog = new CustomDialog(context);
customDialog.setTitle(title);
customDialog.setMessage(message);
customDialog.setYesOnClickListener("确定", new CustomDialog.onYesOnClickListener() {
@Override
public void onYesClick() {
if (onBottomClickListener != null) {
onBottomClickListener.positive();
}
customDialog.dismiss();
}
});
customDialog.setNoOnClickListener("取消", new CustomDialog.onNoClickListener() {
@Override
public void onNoClick() {
if (onBottomClickListener != null) {
onBottomClickListener.negative();
}
customDialog.dismiss();
}
});
customDialog.show();
}
public void show2(final Context context, String title, String message, String yes, String no, final OnBottomClickListener onBottomClickListener) {
customDialog = new CustomDialog(context);
customDialog.setTitle(title);
customDialog.setMessage(message);
customDialog.setYesOnClickListener(yes, new CustomDialog.onYesOnClickListener() {
@Override
public void onYesClick() {
if (onBottomClickListener!=null){
onBottomClickListener.positive();
}
customDialog.dismiss();
}
});
customDialog.setNoOnClickListener(no, new CustomDialog.onNoClickListener() {
@Override
public void onNoClick() {
if (onBottomClickListener!=null){
onBottomClickListener.negative();
}
customDialog.dismiss();
}
});
customDialog.show();
}
public interface OnBottomClickListener {
void positive();
void negative();
}
}
在Activity或者Fragment中使用:
activity_main.xml
<RelativeLayout
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="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="ShowDialog"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/button"
android:layout_marginTop="5dp"
android:text="ShowDialog2"/>
</RelativeLayout>
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button button, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShowDialog showDialog = new ShowDialog();
showDialog.show(MainActivity.this, "Dialog", "自定义Dialog", new ShowDialog.OnBottomClickListener() {
@Override
public void positive() {
Toast.makeText(MainActivity.this, "Click yes", Toast.LENGTH_SHORT).show();
}
@Override
public void negative() {
Toast.makeText(MainActivity.this, "Click no", Toast.LENGTH_SHORT).show();
}
});
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShowDialog showDialog = new ShowDialog();
showDialog.show2(MainActivity.this, "Dialog", "自定义Dialog", "Yes", "No", new ShowDialog.OnBottomClickListener() {
@Override
public void positive() {
Toast.makeText(MainActivity.this, "Yes", Toast.LENGTH_SHORT).show();
}
@Override
public void negative() {
Toast.makeText(MainActivity.this, "No", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}