最近一直在搞自定义的AlertDialog,先贴一下图。
这几种都是自定义的AlertDialog,还支持从Service弹出。好了,话不多说,看使用方法:
CustomDialogBuilder.With(this)//上下文 .load(R.layout.dialog_loading)//自定义的布局文件 .setDimAmount(0.5f)//设置背景透明度 .setCancelable(true)//是否点击区域外隐藏 .setHeigh(DensityUtil.dip2px(this,100))//设置高度 .setWidth(DensityUtil.dip2px(this,100))//设置宽度 .setWidth(0.5f)//设置宽度,以屏幕宽度为基准,取百分比 .InitView(null)//初始化View .InitEvent(null)//初始化事件 .show();//显示
是不是觉得简单好多,我们关心的只有,我们该关心的,至于其他的繁琐的东西,我们不关心好伐。
我们这里看一下是怎么写的,下面会贴出GitHub链接
CustomDialogBuilder.java
import android.app.Activity;import android.app.Fragment;import android.content.Context;import android.support.annotation.LayoutRes;import android.support.v4.app.FragmentActivity;import android.support.v7.app.AlertDialog;import com.xiaolei.easyfreamwork.R;import com.xiaolei.easyfreamwork.application.ApplicationBreage;import com.xiaolei.easyfreamwork.utils.Log;/*** Created by xiaolei on 2017/10/9.*/public class CustomDialogBuilder{ private boolean isBackNet = false; private AlertDialog.Builder builder; private Context context; private CustomDialogBuilder(Context context) { this.context = context; if (Activity.class.isInstance(context) || FragmentActivity.class.isInstance(context)) { if ((Activity.class.isInstance(context) && !((Activity) context).isFinishing()) || (FragmentActivity.class.isInstance(context) && !((FragmentActivity) context).isFinishing())) { builder = new AlertDialog.Builder(context); } else { Log.e("LOG", "不是activity,或者activity已关闭"); } } else { builder = new AlertDialog.Builder(ApplicationBreage.getInstance().getContext(), R.style.Base_Theme_AppCompat_Light_Dialog); isBackNet = true; } } public static CustomDialogBuilder With(Activity activity) { return new CustomDialogBuilder(activity); } public static CustomDialogBuilder With(FragmentActivity activity) { return new CustomDialogBuilder(activity); } public static CustomDialogBuilder With(Fragment fragment) { return new CustomDialogBuilder(fragment.getActivity()); } public static CustomDialogBuilder With(android.support.v4.app.Fragment fragment) { return new CustomDialogBuilder(fragment.getActivity()); } public static CustomDialogBuilder With(Context context) { return new CustomDialogBuilder(context); } /** * 加载布局文件 * * @param layout */ public DialogInitConfig load(@LayoutRes int layout) { return new DialogInitConfig(layout,builder,isBackNet,context); } public Context getContext() { return context; }}
DialogInitConfig.java
import android.content.Context;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.support.annotation.FloatRange;import android.support.annotation.LayoutRes;import android.support.v7.app.AlertDialog;import android.util.DisplayMetrics;import android.view.Gravity;import android.view.View;import android.view.Window;import android.view.WindowManager;/** * * Created by xiaolei on 2017/10/9. */public class DialogInitConfig{ private DialogInitCallBack initCallBack; private DialogEventCallBack eventCallBack; private int width = -1, heigh = -1; private float dimAmount = 0f; @LayoutRes private int layout = -1; private AlertDialog.Builder builder; private boolean isBackNet = false; private boolean cancelable = true; private Context context; public DialogInitConfig(@LayoutRes int layout, AlertDialog.Builder builder, boolean isBackNet, Context context) { this.layout = layout; this.builder = builder; this.isBackNet = isBackNet; this.context = context; } /** * 获取上下文 * @return */ public Context getContext() { return context; } /** * 初始化UI * @param callBack * @return */ public DialogInitConfig InitView(DialogInitCallBack callBack) { initCallBack = callBack; return this; } /** * 初始化事件 * @param callBack * @return */ public DialogInitConfig InitEvent(DialogEventCallBack callBack) { eventCallBack = callBack; return this; } /** * 设置宽度,绝对的值 * @param width * @return */ public DialogInitConfig setWidth(int width) { this.width = width; return this; } /** * 设置宽度,屏幕的百分比的 * @param widthPercent * @return */ public DialogInitConfig setWidth(@FloatRange(from = 0.0,to = 1.0) double widthPercent) { WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); this.width = (int) (wm.getDefaultDisplay().getWidth() * widthPercent); return this; } /** * 设置高度 * @param heigh * @return */ public DialogInitConfig setHeigh(int heigh) { this.heigh = heigh; return this; } /** * 设置背景透明度 * @param dimAmount * @return */ public DialogInitConfig setDimAmount(@FloatRange(from = 0.0,to = 1.0) float dimAmount) { this.dimAmount = dimAmount; return this; } /** * 设置是否可以点击侧边取消 * @param cancelable * @return */ public DialogInitConfig setCancelable(boolean cancelable) { this.cancelable = cancelable; return this; } /** * 显示出来 * @return 返回一个dialog,方便dismiss */ public AlertDialog show() { if (builder == null) { return null; } builder.setTitle(""); builder.setMessage(""); builder.setCancelable(cancelable); AlertDialog alertDialog = builder.create(); if (alertDialog == null) { return null; } Window window = alertDialog.getWindow(); if (window != null) { WindowManager.LayoutParams params = window.getAttributes(); // 去除四角黑色背景 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); if (isBackNet) { window.setType(WindowManager.LayoutParams.TYPE_TOAST); } window.setAttributes(params); } alertDialog.show(); try { View view = View.inflate(alertDialog.getContext(), layout, null); alertDialog.setContentView(view); if (initCallBack != null) { initCallBack.onInit(alertDialog, view); } if (eventCallBack != null) { eventCallBack.initEvent(alertDialog, view); } window = alertDialog.getWindow(); if (window != null) { WindowManager.LayoutParams params = window.getAttributes(); window.setGravity(Gravity.CENTER); DisplayMetrics dm = new DisplayMetrics(); window.getWindowManager().getDefaultDisplay().getMetrics(dm); if (width != -1) { params.width = width; } if (heigh != -1) { params.height = heigh; } // 设置周围的暗色系数 params.dimAmount = dimAmount; window.setAttributes(params); } } catch (Exception e) { e.printStackTrace(); } return alertDialog; } public interface DialogInitCallBack { public void onInit(AlertDialog alertDialog, View view); } public interface DialogEventCallBack { public void initEvent(AlertDialog alertDialog, View view); }}
好吧其实我也知道你们根本不会看这些代码的,我就是要贴出来。
这里贴GitHub链接 :
The End