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

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

当前回答

ColorDrawable drawable = new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent));
dialog.getWindow().setBackgroundDrawable(drawable);

其他回答

不知何故,撒迦利亚的解决方案对我不起作用,所以我用下面的主题来解决这个问题…

<style name="DialogCustomTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>

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

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

享受! !

如果你扩展了DialogFrament类,你可以设置主题:

setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);

然后在styles.xml文件中创建自定义主题(参见@LongLv的参数回答)

不要忘记添加<item name="android:windowCloseOnTouchOutside">true</item>如果你想让用户在对话框外触摸时关闭对话框。

与zGnep相同的解决方案,但使用xml:

android:background="@null"

你可以使用:

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

在我的案例中,解决方案是这样的:

final Drawable drawable = new ColorDrawable(android.graphics.Color.TRANSPARENT);
dialogAssignTag.getWindow().setBackgroundDrawable(drawable);

另外,在自定义对话框的xml文件中:

android:alpha="0.8"