在Android中使用透明背景的对话框

Pro*_*mer 230 android android-dialog

如何从Android中的对话框中删除黑色背景.图片显示了问题.

在此输入图像描述

final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger); 
Run Code Online (Sandbox Code Playgroud)

Zac*_*uel 655

添加此代码

 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Run Code Online (Sandbox Code Playgroud)

编辑

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!但是,我更喜欢使用`dialog.getWindow().setBackgroundDrawable(new ColorDrawableResource(R.color.transparent));` (14认同)
  • 我更喜欢使用`dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);` (13认同)
  • 这个解决方案有帮 问题是,宽度将适合屏幕.与普通对话框相比,没有填充.但Android 4.1默认处理它 (4认同)

Lon*_*gLv 78

<style name="NewDialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在java中使用

Dialog dialog = new Dialog(this, R.style.NewDialog);
Run Code Online (Sandbox Code Playgroud)

希望对你有所帮助!

  • @John您可以使用:dialog.setCanceledOnTouchOutside(true); (2认同)

Fah*_*que 30

我遇到了一个更简单的问题,我想出的解决方案是应用透明的bachground主题.在你的风格中写下这些线条

    <item name="android:windowBackground">@drawable/blue_searchbuttonpopupbackground</item>
</style>
<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后添加

android:theme="@style/Theme.Transparent"
Run Code Online (Sandbox Code Playgroud)

在主清单文件中,在对话框活动的块内.

另外还有对话框活动XML集

 android:background= "#00000000"
Run Code Online (Sandbox Code Playgroud)

  • 或者如果你只想设置Dialog的样式,请使用它:<style name ="Theme.Transparent"parent ="@ android:style/Theme.Dialog"> (2认同)

Rav*_*rma 14

不知何故Zacharias解决方案对我没有用,所以我用下面的主题来解决这个问题......

<style name="DialogCustomTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>
Run Code Online (Sandbox Code Playgroud)

可以将此主题设置为对话框,如下所示

final Dialog dialog = new Dialog(this, R.style.DialogCustomTheme); 
Run Code Online (Sandbox Code Playgroud)

请享用!!


zio*_*npi 12

你可以使用:

setBackgroundDrawable(null);
Run Code Online (Sandbox Code Playgroud)

方法.以下是文档:

  /**
    * Set the background to a given Drawable, or remove the background. If the
    * background has padding, this View's padding is set to the background's
    * padding. However, when a background is removed, this View's padding isn't
    * touched. If setting the padding is desired, please use
    * {@link #setPadding(int, int, int, int)}.
    *
    * @param d The Drawable to use as the background, or null to remove the
    *        background
    */
Run Code Online (Sandbox Code Playgroud)


erf*_*fan 12

如果你想破坏对话框的深色背景,使用这个

dialog.getWindow().setDimAmount(0);
Run Code Online (Sandbox Code Playgroud)


dug*_*ggu 11

对话框弹出填充默认的黑色背景颜色或主题颜色,因此您需要将TRANSPARENT背景设置为Dialog.尝试以下代码: -

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.splash);
dialog.show();
Run Code Online (Sandbox Code Playgroud)


Ivo*_*Liu 8

我在所有现有答案中发现的一个问题是边距不会被保留.这是因为它们都android:windowBackground使用纯色覆盖了负责边距的属性.但是,我在Android SDK中进行了一些挖掘,发现默认窗口背景是可绘制的,并对其进行了一些修改以允许透明对话框.

首先,将/platforms/android-22/data/res/drawable/dialog_background_material.xml复制到您的项目中.或者,只需将这些行复制到新文件中:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:inset="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="?attr/colorBackground" />
    </shape>
</inset>
Run Code Online (Sandbox Code Playgroud)

请注意,android:color设置为?attr/colorBackground.这是您看到的默认灰色/白色.要允许android:background自定义样式中定义的颜色透明并显示透明度,我们所要做的就是更改?attr/colorBackground@android:color/transparent.现在看起来像这样:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:inset="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@android:color/transparent" />
    </shape>
</inset>
Run Code Online (Sandbox Code Playgroud)

之后,转到您的主题并添加:

<style name="MyTransparentDialog" parent="@android:style/Theme.Material.Dialog">
    <item name="android:windowBackground">@drawable/newly_created_background_name</item>
    <item name="android:background">@color/some_transparent_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

确保替换newly_created_background_name为刚刚创建的可绘制文件的实际名称,并替换some_transparent_color为所需的透明背景.

之后,我们需要做的就是设置主题.创建时使用此选项AlertDialog.Builder:

    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);
Run Code Online (Sandbox Code Playgroud)

然后像往常一样构建,创建和显示对话框!


Ras*_*iri 7

注意:不要使用构建器来更改背景。

Dialog dialog = new Dialog.Builder(MainActivity.this)
                                .setView(view)
                                .create();
dialog.show();dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Run Code Online (Sandbox Code Playgroud)

改成

Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
Run Code Online (Sandbox Code Playgroud)

使用 Dialog.builder 时,它没有提供getWindow()选项。