Dialog with custom view, setCanceledOnTouchOutside doesn't work

后端 未结 2 2071
遇见更好的自我
遇见更好的自我 2021-01-07 04:15

am using a Dialog with a custom view. That view\'s layout is 300dp x 300dp is size and centered on the screen. Now i want it to be dismissed when the user touches outside th

相关标签:
2条回答
  • 2021-01-07 04:40

    i did solve it now by wrapping my custom dialog view layout with an additional LinearLayout and adding an onTouchListener to this wrapping LinearLayout. In that onTouchListener i checked if the click was inside or outside the dialog.

    0 讨论(0)
  • 2021-01-07 04:57

    It might be a tad late but for those who are still looking for the answer, here it is.

    Apply a custom style to your dialog. In your style, include this:

    <item name="android:windowIsFloating">true</item>
    

    Otherwise the dialog will use full screen. Hence no outside area to click to cancel. This works for dialogs with custom content.

    Example:

    <style name="dialog_theme" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center</item>
    </style>
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题