有什么方法可以删除 AlertDialog 的背景吗?

Is there any way to remove the background of an AlertDialog?

我正在尝试更改 Android AlertDialog 的背景颜色。

我见过的每个 post 都是关于更改 AlertDialog 的颜色,而不是背后的背景。

如有任何帮助,我们将不胜感激。

你可以减少暗淡量

dialog.getWindow().setDimAmount(0.5f);

要获得背景颜色,您需要制作自定义警报对话框。

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>

然后在Builder构造函数中应用:

AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
    ...
    .create();

有关详细信息,请参阅 see this post

有一种方法可以满足您的要求,但这不是一个很好的解决方案

首先,像这样设置自定义对话框主题。

styles.xml

    <style name="CustomDialogTheme" parent="android:Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

CustomDialog.java

应用主题并构建您的 custom_dialog 视图,如下所示。

 public HTCustomDialog(Context context) {
    super(context, R.style.CustomDialogTheme);
    setContentView(R.layout.custom_dialog);
}

custom_dialog.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_solid_80">

    <RelativeLayout
        android:id="@+id/dialog_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/bg_popup"
        android:padding="16dp">

</RelativeLayout>

既然 CustomDialog 视图是全屏视图,请将根布局的背景设置为您喜欢的任何颜色。

我从 那里得到了这个答案,请看一下。