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

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

当前回答

您可以使用(可选)

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

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

import android.app.Dialog

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

并在任何对话中使用它

dialog.setTransparentBackground()

做一些有趣的编程……

其他回答

这就是我用AlertDialog实现半透明的方法。

创建自定义样式:

<style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
    <item name="android:colorBackground">#32FFFFFF</item>
</style>

然后创建对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
AlertDialog dialog = builder.create();

你可以使用:

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

注意:不要使用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()选项。

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

在你的代码中试试这个:

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

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