Android打造万能的对话框Dialog(三)

打造万能的对话框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();

            }
        });

总结

可能很多人会说,为什么全是代码。一点废话都没有,我想说的是,也许真的是太简单了,所以就略过白话问了。如果还是不懂,代码奉上。

源码下载

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值