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

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

当前回答

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

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

享受! !

其他回答

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

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

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

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

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

android:alpha="0.8"

您可以使用(可选)

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

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

import android.app.Dialog

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

并在任何对话中使用它

dialog.setTransparentBackground()

做一些有趣的编程……

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

dialog.getWindow().setDimAmount(0);

注意:不要使用builder来改变背景。

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

改变

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

使用对话框时。builder,它没有给出getWindow()选项。