美文网首页
Dialog设置无标题并可点击外部

Dialog设置无标题并可点击外部

作者: Erich_Godsen | 来源:发表于2021-03-24 17:55 被阅读0次

Dialog通用属性设置

<style name="DialogStyle" parent="Theme.AppCompat.Dialog">
        <!-- 进入和退出时动画-->
        <item name="android:windowAnimationStyle">@style/AnimBottom</item> 
        <!--边框-->
        <item name="android:windowFrame">@null</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--半透明-->
        <item name="android:windowIsTranslucent">true</item>
        <!--无标题-->
        <item name="android:windowNoTitle">true</item>
        <!--去掉Dialog的标题-->
        <item name="windowNoTitle">true</item>
        <!--背景透明-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--背景是否变暗-->
        <item name="android:backgroundDimEnabled">false</item>
        <!-- 点击空白区域是否消失  API Level>=11-->
        <item name="android:windowCloseOnTouchOutside">false</item>   
    </style>

让Dialog外部区域可以正常响应事件

onCreate方法中给Window设置如下属性

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

如果当前Dialog关心外部的事件,可设置如下属性

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

设置完以后,当外部触发事件时会在当前DialogonTouchEvent中下发
MotionEvent.ACTION_OUTSIDE 事件,但是需要注意一点,仅在第一次按下时会下发此事件,不能完整接收整个事件过程,另外这两个Flag的注释如下,大家有兴趣的可以看一下

        /** Window flag: even when this window is focusable (its
         * {@link #FLAG_NOT_FOCUSABLE} is not set), allow any pointer events
         * outside of the window to be sent to the windows behind it.  Otherwise
         * it will consume all pointer events itself, regardless of whether they
         * are inside of the window. */
        public static final int FLAG_NOT_TOUCH_MODAL    = 0x00000020;

        /** Window flag: if you have set {@link #FLAG_NOT_TOUCH_MODAL}, you
         * can set this flag to receive a single special MotionEvent with
         * the action
         * {@link MotionEvent#ACTION_OUTSIDE MotionEvent.ACTION_OUTSIDE} for
         * touches that occur outside of your window.  Note that you will not
         * receive the full down/move/up gesture, only the location of the
         * first down as an ACTION_OUTSIDE.
         */
        public static final int FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000;

相关文章

网友评论

      本文标题:Dialog设置无标题并可点击外部

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