当我在外面的对话框附近点击时,setCanceledOnTouchOutside不起作用

时间:2013-08-14 15:01:20

标签: android touch alertdialog

我有一个AlertDialog:

        AlertDialog.Builder builder = new AlertDialog.Builder(context,       AlertDialog.THEME_HOLO_LIGHT);
    ListAdapter adapter = new CustomDialogAdapter(context, itemsList);
    builder.setAdapter(adapter, listener);
    builder.setTitle(title);
    AlertDialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(true);

setCanceledOnTouchOutside仅在我点击对话框一定距离时才有效。当我在对话框附近点击时它不会被解雇。你们知道一种解除对话的方法,即使我在对话框附近点击?谢谢。

1 个答案:

答案 0 :(得分:1)

如果您希望对话框全屏,则使用AlertDialog.THEME_HOLO_LIGHT。另一种方法是创建自己的样式,如下所示:

  AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.ThemeDialogCustom);
    ListAdapter adapter = new CustomDialogAdapter(context, itemsList);
    builder.setAdapter(adapter, listener);
    builder.setTitle(title);
    AlertDialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(true);

在values文件夹中有style.xml,如下面的:::

<?xml version="1.0" encoding="utf-8"?>

<resources>
<style name="ThemeDialogCustom" parent="android:Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowBackground">@color/transparent_color</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
   </style>

</resources>

还在values folder :::

中添加colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="transparent_color">#00000000</color>
</resources>

这对我有用。希望它也适合你