Android打造万能的对话框Dialog(三)
2017-05-27 16:06
351 查看
打造万能的对话框Dialog(三)
已经写了两篇了,这下来一篇高级点的,仿照AlertDialog封装我们想要的Dialog的对话框具体那种方式跟适合你,你自己来选,不过如果只是为了简单实用,我感觉上一篇的封装方式也许更合适
先看图,没图说个毛线
简单介绍
前面也说了,我们这次要玩一个高级的,仿照Android系统提供AlertDialog的来写。首先、我们怎么着也得先看一下源码吧(这个自己看吧,就不带领你们来看了)AlertDialog源码总结
1、建立Builder类,这个是为了构建和设置参数的(这里说明一下,最好是了解一下建造者模式)2、建立Dialog所需要的参数类
3、参数和对话框进行数据绑定
4、搞定,进行使用就可以(同AlertDialog用法相同)
建立Builder类
public static class Builder { private DialogParams P; public Builder(Context context){ // 如果没有传默认的就是自定义的主题 this(context, 0); } public Builder(Context context,int themeResId){ P = new DialogParams(context,themeResId); } /** * Set a custom view resource to be the contents of the Dialog. The * resource will be inflated, adding all top-level views to the screen. * * @param layoutResId Resource ID to be inflated. * @return this Builder object to allow for chaining of calls to set * methods */ public Builder setView(int layoutResId) { P.mView = null; P.mViewLayoutResId = layoutResId; return this; } /** * Sets a custom view to be the contents of the alert dialog. * <p> * When using a pre-Holo theme, if the supplied view is an instance of * a {@link ListView} then the light background will be used. * <p> * <strong>Note:</strong> To ensure consistent styling, the custom view * should be inflated or constructed using the alert dialog's themed * context obtained via {@link #getContext()}. * * @param view the view to use as the contents of the alert dialog * @return this Builder object to allow for chaining of calls to set * methods */ public Builder setView(View view) { P.mView = view; P.mViewLayoutResId = 0; return this; } /** * 设置宽度 */ public Builder setWidth(int width){ P.mWidth = width; return this; } /** * 设置高度 */ public Builder setHeight(int height){ P.mHeight = height; return this; } /** * 显示动画 */ public Builder showAnimationFromBottom(){ P.mAnimation = R.style.main_menu_animstyle; P.mGravity = Gravity.BOTTOM; return this; } public Builder setGravity(int gravity){ P.mGravity = gravity; return this; } /** * 显示动画 可以自定动画 */ public Builder showAnimation(int styleResId){ P.mAnimation = styleResId; return this; } public CommonDialog create() { // Context has already been wrapped with the appropriate theme. final CommonDialog dialog = new CommonDialog(P.context, P.themeResId); // 绑定一些参数 dialog.apply(P); return dialog; }
建立参数类
public static class DialogParams{ public Context context; public int themeResId; public View mView; public int mViewLayoutResId; public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT; public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT; public int mAnimation = 0; public int mGravity = Gravity.CENTER; public DialogParams(Context context, int themeResId) { this.context = context; this.themeResId = themeResId; } }
参数和对话框进行数据绑定
/** * 绑定和设置参数 */ private void apply(Builder.DialogParams P) { mDialogView = P.mView;// 资源 View if(mDialogView == null && P.mViewLayoutResId == 0){ throw new NullPointerException("大哥请设置布局!!!"); } mDialogView = View.inflate(P.context,P.mViewLayoutResId,null); // 设置布局 setContentView(mDialogView); // 设置基本参数 Window window = getWindow(); window.setLayout(P.mWidth, P.mHeight); window.setGravity(P.mGravity); if(P.mAnimation != 0) { window.setWindowAnimations(P.mAnimation); } }
使用
findViewById(R.id.btn_center).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CommonDialog.Builder builder = new CommonDialog.Builder(MainActivity.this,R.style .DialogTheme); builder.setView(R.layout.dialog_center) .setGravity(Gravity.CENTER); CommonDialog dialog = builder.create(); dialog.show(); } });
总结
可能很多人会说,为什么全是代码。一点废话都没有,我想说的是,也许真的是太简单了,所以就略过白话问了。如果还是不懂,代码奉上。源码下载
源码下载相关文章推荐
- Android打造万能的对话框Dialog(二)
- Android打造万能的对话框Dialog(一)
- Android之UI--打造万能自定义Dialog
- Android Dialog对话框样式设定-最简单,万能有效方法
- Android之UI--打造12种Dialog对话框
- Android之UI--打造12种Dialog对话框
- Android之UI--打造12种Dialog对话框
- 20. android dialog——自定义对话框之一
- 系出名门Android(3) - 对话框(Dialog)和通知(Notification)
- Android 对话框(Dialog)大全 建立你自己的对话框
- 19. android dialog —— 日期和时间选择对话框
- Android AlertDialog 方法setView(view,0,0,0,0)开发自定义对话框
- Android 对话框(Dialog)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog) 推荐
- Android Dialog( 自定义对话框)
- 14. android dialog——普通对话框原理
- Android 对话框(Dialog)大全 建立你自己的对话框
- Android 对话框【Dialog】去除白色边框代码
- Android 对话框(Dialog)大全 建立你自己的对话框(转)
- Android用户界面——对话框(Dialog)