如何更改Dialog的默认黑色暗淡背景"颜色"(不是暗淡的数量)?

Lee*_*eol 13 android dialog

在此输入图像描述

(这是Dialog在互联网上显示找到的随机图像.)

我一直在实施自定义Dialog.我可以处理对话框上的几乎所有内容,除了对话框本身下的默认黑色暗淡背景,但是在它后面的整个屏幕上.基本上我想改变它的颜色和alpha值.

我一直在浏览StackOverflow,但我发现只有改变Dialog自身背景的答案.无论如何,如果你需要它,这是我的简单代码.

CustomDialog.java

public class HTDialog extends Dialog{

    public HTDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.custom_dialog);
    }
}
Run Code Online (Sandbox Code Playgroud)

custom_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="280dp"
    android:background="@drawable/bg_popup"
    android:paddingTop="20dp">

    <ImageView
        android:id="@+id/dialog_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/icon" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Lee*_*eol 24

这是一种解决方法,但它并不是一个纯粹的解决方案,因为后台触摸被禁用并应手动配置.

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

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>
Run Code Online (Sandbox Code Playgroud)

设置windowIsFloating为false会强制Dialog视图扩展到全屏.设置windowBackgroundtransparent删除默认的黑色暗淡背景Dialog.windowNoTitle选项摆脱上标题栏.

CustomDialog.java

应用主题并构建custom_dialog视图,如下所示.

public HTCustomDialog(Context context) {
    super(context, R.style.CustomDialogTheme);
    setContentView(R.layout.custom_dialog);
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

现在该CustomDialog视图是一个全屏视图,将background根布局设置为您想要的任何颜色.

样本结果

我把结果拼凑了一下.

结果