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

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

当前回答

对话框弹出填充默认的黑色背景颜色或主题颜色,所以你需要设置透明的背景到对话框。试试下面的代码:-

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();

其他回答

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

您可以使用(可选)

dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

我建议创建一个扩展函数。类似的 extensions.kt

import android.app.Dialog

fun Dialog.setTransparentBackground() {
    window?.setBackgroundDrawableResource(android.R.color.transparent)
}

并在任何对话中使用它

dialog.setTransparentBackground()

做一些有趣的编程……

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

使用这段代码,它是为我工作:

Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();