如何在主题透明时取消对话框?

时间:2012-04-03 06:08:11

标签: android dialog

Pls see the code below..



 final Dialog mDialog = new Dialog(this);
  mDialog.setCanceledOnTouchOutside(true); //it works

  mDialog.setContentView(R.layout.popup);
  mDialog.show(); 

但是如果我为对话添加主题那么

      final Dialog mDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
  mDialog.setCanceledOnTouchOutside(true); //But here It is not workin
      mDialog.setContentView(R.layout.popup);
  mDialog.show(); 

popup.xml

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

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
 android:id="@+id/lyt_popup"
 android:layout_gravity="center"
 android:background="@drawable/popup_background"
  android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
     <Button android:layout_height="wrap_content" android:background="@drawable/play_game"      android:id="@+id/btn_play" android:layout_width="wrap_content"  android:layout_alignParentTop="true"   android:layout_centerHorizontal="true" android:layout_marginTop="96dp" android:layout_gravity="center_horizontal">   </Button>
  <Button android:background="@drawable/memory_game" android:id="@+id/btn_memory"   android:layout_width="wrap_content" android:layout_height="wrap_content"  a   ndroid:layout_gravity="center_horizontal"></Button>

 </LinearLayout>

2 个答案:

答案 0 :(得分:2)

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

<style
    name="Theme_Dialog_Translucent"
    parent="android:Theme.Dialog">
    <item
        name="android:windowBackground">@null</item>
</style>

以下是工作代码::

Dialog mDialog = new Dialog(this,R.style.ThemeDialogCustom);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setCanceledOnTouchOutside(true); //But here It is not workin
mDialog.setContentView(R.layout.popup);
mDialog.show(); 

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

答案 1 :(得分:0)

而不是mDialog.setCanceledOnTouchOutside(true); //但在这里它不起作用,

应为mDialog.setCancelable(true);