对话框与自定义视图,setCanceledOnTouchOutside不起作用自定义、视图、对话框、不起作用

2023-09-06 06:50:02 作者:老梗

现在用一个对话框,自定义视图。这种观点的布局300dp x 300dp的大小,居中显示在屏幕上。现在我想,当用户触摸对话外就被解雇。我想:太好了!有一个函数为这样:setCanceledOnTouchOutside(布尔值)。但我没有任何影响。它根本不起作用。 我也尝试添加一个onTouchListener和侦听的对话之外的事件,但这些事件似乎并没有被监听器捕获:(

任何想法,为什么这不起作用或者它如何能以另一种方式来完成?

下面是我如何声明对话框:

  mMenuDialog =新的对话框(这一点,
            android.R.style.Theme_Translucent_NoTitleBar);
    mMenuDialog.setContentView(R.layout.menu_dialog);
    mMenuDialog.setCancelable(真正的);
    mMenuDialog.setCanceledOnTouchOutside(真正的);
 

和这里的自定义视图的声明

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =300dp机器人:layout_height =300dp
机器人:背景=@可绘制/ menu_dialog_background
机器人:方向=垂直机器人:layout_gravity =中心
机器人:ID =@ + ID / dialog_root>
...
< / LinearLayout中>
 
Excel怎样自定义视图和打印区域

和这里是用来背景:

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<填充的android:左=15dp机器人:顶部=15dp机器人:右=15dp
    机器人:底部=15dp/>
[固体机器人:颜色=#F0F0F0/>
<边角机器人:半径=15dip/>
< /形状>
 

解决方案

这可能是一点点晚,但对于那些谁仍然在寻找的答案,在这儿呢。

应用自定义样式到您的对话框。在你的风格,包括这样的:

 <项目名称=机器人:windowIsFloating>真< /项目>
 

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

例如:

 <样式名称=dialog_theme父=@安卓风格/ Theme.Dialog>
    <项目名称=机器人:windowBackground> @android:彩色/透明< /项目>
    <项目名称=机器人:windowIsFloating>真< /项目>
    <项目名称=机器人:windowNoTitle>真< /项目>
    <项目名称=机器人:layout_width> WRAP_CONTENT< /项目>
    <项目名称=机器人:layout_height> WRAP_CONTENT< /项目>
    <项目名称=机器人:layout_gravity>中心< /项目>
< /风格>
 

希望这有助于。

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.

 
精彩推荐