如何从Android对话框中删除黑色背景。这张图片显示了问题所在。

final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger); 

当前回答

你可以使用:

setBackgroundDrawable(null);

方法。下面是文档:

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

其他回答

TL,博士;你只需要两件事,首先在你的风格做一些像这样的事情:

<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>

其次,100%确保上述样式应用到对话框中(可能通过传递给构造函数)。

完整的示例

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

在Java中使用:

Dialog dialog = new Dialog(this, R.style.NewDialog);

希望对你有所帮助!

如果你使用Kotlin,这段代码可以帮助你:

Objects.requireNonNull(dialog.window)
                ?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

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

dialog.getWindow().setDimAmount(0);

在你的代码中试试这个:

getWindow().setBackgroundDrawableResource(android.R.color.transparent);

它肯定会起作用……对我来说…!我的弗洛伊德

Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

这是我的方法,你可以试试!