具有自定义视图的对话框,setCanceledOnTouchOutside不起作用[英] Dialog with custom view, setCanceledOnTouchOutside doesn't work

本文是小编为大家收集整理的关于具有自定义视图的对话框,setCanceledOnTouchOutside不起作用的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

正在使用具有自定义视图的对话框.该视图的布局是300dp x 300dp尺寸,居中居中.现在,当用户触摸对话框外时,我希望它被解雇.我想:很棒!有一个函数:setCancelEdontouchOutside(布尔值).但我没有效果.它根本不起作用. 我还尝试添加一个OntouchListener,并侦听对话框外的事件,但这些事件似乎没有被侦听器捕获:(

为什么这不起作用的任何想法或如何以另一种方式完成?

这是我如何声明对话框:

mMenuDialog = new Dialog(this,
            android.R.style.Theme_Translucent_NoTitleBar);
    mMenuDialog.setContentView(R.layout.menu_dialog);
    mMenuDialog.setCancelable(true);
    mMenuDialog.setCanceledOnTouchOutside(true);

而且以下是自定义视图的声明

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp" android:layout_height="300dp"
android:background="@drawable/menu_dialog_background"
android:orientation="vertical" android:layout_gravity="center"
android:id="@+id/dialog_root">
...
</LinearLayout>

和这里是二手背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="15dp" android:top="15dp" android:right="15dp"
    android:bottom="15dp" />
<solid android:color="#f0f0f0" />
<corners android:radius="15dip" />
</shape> 

推荐答案

可能是一个tad,但对于那些仍在寻找答案的人来说,这里是.

将自定义样式应用于对话框.在您的风格中,包括:

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

否则对话框将使用全屏.因此,单击以取消以外的区域.这适用于具有自定义内容的对话框.

示例:

<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>

希望这有帮助.

其他推荐答案

我现在通过用附加的linearlayout包装我的自定义对话视图布局并将ontouchlistener添加到此包装的linearlayout.在该ontouchlistener中,我检查了点击内部或在对话框内部.

本文地址:https://www.itbaoku.cn/post/102702.html

问题描述

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 the dialog. I thought: great! there is a function for this: setCanceledOnTouchOutside(boolean). But i has no effect. It simply doesn't work. I also tried to add an onTouchListener and listen for events that are outside the dialog, but those events doesn't seem to be captured by the listener :(

Any ideas why this doesn't work or how it could be done in another way?

Here is how i declare the Dialog:

mMenuDialog = new Dialog(this,
            android.R.style.Theme_Translucent_NoTitleBar);
    mMenuDialog.setContentView(R.layout.menu_dialog);
    mMenuDialog.setCancelable(true);
    mMenuDialog.setCanceledOnTouchOutside(true);

and here is the declaration of the custom view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp" android:layout_height="300dp"
android:background="@drawable/menu_dialog_background"
android:orientation="vertical" android:layout_gravity="center"
android:id="@+id/dialog_root">
...
</LinearLayout>

and here is the used background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="15dp" android:top="15dp" android:right="15dp"
    android:bottom="15dp" />
<solid android:color="#f0f0f0" />
<corners android:radius="15dip" />
</shape> 

推荐答案

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.

其他推荐答案

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.