话不多说直接上图
首先对布局的搭建
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="提示"
android:textColor="#333333"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:padding="5dp"
android:text="提示的内容"
android:textColor="#333333"
android:textSize="14sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="#eeeeee" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_cancle"
android:textColor="#333333"
android:padding="8dp"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
<TextView
android:id="@+id/tv_confirm"
android:text="确定"
android:textColor="#62b7f6"
android:padding="8dp"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
自定义背景dialog_bg.xml代码如下
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="#eeeeee" />
<corners android:radius="8dp" />
</shape>
弹出框的style设置,在res下values下styles.xml里写代码,如果没有styles.xml文件就自己创建一个,具体代码如下
<!-- 提示弹出框-->
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!--边框-->
<item name="android:windowIsFloating">true</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item>
<!--半透明-->
<item name="android:windowNoTitle">true</item>
<!--无标题-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--背景透明-->
<item name="android:backgroundDimEnabled">true</item>
<!--模糊-->
</style>
以上准备工作已完成,下面就是对dialog封装
package cn.xu.test.utils;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import cn.xu.test.R;
/**
* Created by Administrator on 2019/9/3.
*/
public class CommomDialog extends Dialog implements View.OnClickListener {
private TextView tvTitle;
private TextView tvContent;
private TextView tvConfirm;
private TextView tvCancle;
private Context mContext;
private String content;
private OnCloseListener listener;
private String title;
private String positiveName;
private String negativeName;
public CommomDialog(Context context) {
super(context);
}
public CommomDialog(Context context, String content) {
super(context, R.style.dialog);
this.mContext = context;
this.content = content;
}
public CommomDialog(Context context, int themeResId, String content) {
super(context, themeResId);
this.mContext = context;
this.content = content;
}
public CommomDialog(Context context, int themeResId, String content, OnCloseListener closeListener) {
super(context, themeResId);
this.mContext = context;
this.content = content;
this.listener = closeListener;
}
protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
this.mContext = context;
}
public CommomDialog setTitle(String title) {
this.title = title;
return this;
}
public CommomDialog setPositiveButton(String name) {
this.positiveName = name;
return this;
}
public CommomDialog setNegativeButton(String name) {
this.negativeName = name;
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.commom_dialog);
setCanceledOnTouchOutside(false);//触摸旁边的屏幕关闭弹出框 false不允许 true允许
initView();
}
private void initView() {
tvTitle = findViewById(R.id.tv_title);
tvContent = findViewById(R.id.tv_content);
tvCancle = findViewById(R.id.tv_cancle);
tvConfirm = findViewById(R.id.tv_confirm);
tvConfirm.setOnClickListener(this);
tvCancle.setOnClickListener(this);
tvContent.setText(content);
if (!TextUtils.isEmpty(negativeName)) {
tvCancle.setText(negativeName);
}
if (!TextUtils.isEmpty(positiveName)) {
tvConfirm.setText(positiveName);
}
if (!TextUtils.isEmpty(title)) {
tvTitle.setText(title);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_confirm:
if (listener != null) {
listener.onClick(this, true);
}
break;
case R.id.tv_cancle:
if (listener != null) {
listener.onClick(this, false);
}
this.dismiss();//关闭对话框
break;
}
}
public interface OnCloseListener {
void onClick(Dialog dialog, boolean confirm);
}
}
以上就是dialog的封装,下面是在activity中应用
package cn.xu.test;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import cn.xu.test.utils.CommomDialog;
public class DialogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
//按钮,点击弹出对话框
public void hint(View view) {
new CommomDialog(this, R.style.dialog, "您确定关闭这个弹出框吗?", new CommomDialog.OnCloseListener() {
@Override
public void onClick(Dialog dialog, boolean confirm) {
if (confirm){
dialog.dismiss();
}
}
}).setTitle("提示").show();
}
}
OK,自定义弹窗已完成!