美文网首页
自定义Dialog原理

自定义Dialog原理

作者: Alien的小窝 | 来源:发表于2017-02-20 21:12 被阅读86次

自定义Dialog有很多种实现方式,有扩展Dialog自身的,基于PopUpWindow的,DialogFragment,自定义View, DecorView等等...

个人认为比较好的方案是直接在DecorView上面添加子View控件实现,下面是两个开源库的地址,实现原理均基于DecorView

https://github.com/orhanobut/dialogplus
https://github.com/Tapadoo/Alerter

获取Activity的DecorView

show

decorView = (ViewGroup) getActivityWeakReference().get().getWindow().getDecorView();
decorView.addView(getAlert());// getAlert是一个FrameLayout(子View)

hide

getAlert()被点击关闭按钮时触发
1.执行动画
2.监听动画结束时从decorView中remove掉自己

((ViewGroup) getParent()).removeView(Alert.this);

注意:在创建Dialog时候,如果前一个Dialog还没有手动关闭,记得判断下自行remove一次
如下

    public static Alerter create(@NonNull final Activity activity) {
        if (activity == null) {
            throw new IllegalArgumentException("Activity cannot be null!");
        }

        final Alerter alerter = new Alerter();

        //Clear Current Alert, if one is Active
        Alerter.clearCurrent(activity);

        alerter.setActivity(activity);
        alerter.setAlert(new Alert(activity));

        return alerter;
    }

    /**
     * Cleans up the currently showing alert view, if one is present
     */
    private static void clearCurrent(@NonNull final Activity activity) {
        if (activity == null) {
            return;
        }

        try {
            final View alertView = activity.getWindow().getDecorView().findViewById(R.id.flAlertBackground);
            //Check if the Alert is added to the Window
            if (alertView == null || alertView.getWindowToken() == null) {
                Log.d(Alerter.class.getClass().getSimpleName(), activity.getString(R.string.msg_no_alert_showing));
            } else {
                //Animate the Alpha
                alertView.animate().alpha(0).withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        //And remove the view for the parent layout
                        ((ViewGroup) alertView.getParent()).removeView(alertView);
                    }
                }).start();

                Log.d(Alerter.class.getClass().getSimpleName(), activity.getString(R.string.msg_alert_cleared));
            }
        } catch (Exception ex) {
            Log.e(Alerter.class.getClass().getSimpleName(), Log.getStackTraceString(ex));
        }
    }

源码比较简单,上面仅仅说明思路,可自行实现适合自己的

相关文章

  • Dialog

    安卓dialog的使用+如何自定义dialog自定义Dialog自定义Dialog 自定义

  • 自定义Dialog

    自定义Dialog的主题 自定义Dialog的布局文件 继承Dialog 并在onCreate方法中将布局设置给D...

  • 自定义Dialog原理

    自定义Dialog有很多种实现方式,有扩展Dialog自身的,基于PopUpWindow的,DialogFragm...

  • 实现图片Dialog中带ViewPager

    效果图 实现思路 自定义Dialog,为Dialog添加自定义布局,自定义PagerAdapter以及PageTr...

  • 【Android】自定义全屏dialog

    一、在themes.xml中添加自定义dialog的样式 二、创建dialog基类 三、创建自定义dialog的布...

  • Android圆角对话框Dialog

    需求:模仿iOS样式Dialog对话框。 自定义Dialog 核心代码: Dialog样式: Dialog布局文件...

  • Android自定义Dialog及其点击事件

    在项目开发中,经常要用到dialog。但是系统的dialog太丑,所有我们要自定义dialog。下面的先介绍自定义...

  • 一个漂亮的自定义Dialog

    这是一个自定义的dialog项目 自定义的dialog,具有如下特点 圆角的dialog View 圆形图片的ti...

  • Flutter Dialog 动画

    本文对 Dialog 做一次系统性学习记录,包括系统 Dialog,自定义 Dialog,Dialog 动画。 A...

  • 自定义Dialog

    仿IOS自定义的Dialog: 1、Util帮助类创建dialog 2、布局文件 :loading_dialog....

网友评论

      本文标题:自定义Dialog原理

      本文链接:https://www.haomeiwen.com/subject/yjnewttx.html