如何更改默认的黑色暗淡背景“颜色"?(不是dim的量)Dialog? [英] How can I change default black dim background "color" (not the amount of dim) of Dialog?

查看:31
本文介绍了如何更改默认的黑色暗淡背景“颜色"?(不是dim的量)Dialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是一张随机图片,显示了在 Internet 上找到的 Dialog.)

(This is a random image of showing a Dialog found on the Internet.)

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

I've been implementing a custom Dialog. I could handle almost everything on the dialog except for that default black dim background under the dialog itself, but over the entire screen behind it. Basically I want to change its color and alpha value.

我一直在 StackOverflow 中徘徊,但我找到的唯一答案是关于更改 Dialog 本身的背景.无论如何,如果您需要它,这是我的简单代码.

I've been wandering through StackOverflow but only answers I found were about changing background of Dialog itself. In any case if you need it, here's my simple codes.

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

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>

推荐答案

这是一种变通方法,但它并不是真正的纯粹解决方案,因为背景触摸已禁用,应手动配置.

This is a workaround but it's not really a pure solution because background touch is disabled and should be configured manually.

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

First, set custom dialog theme like this.

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

windowIsFloating 设置为 false 会强制 Dialog 视图扩展到全屏.将 windowBackground 设置为 transparent 会移除 Dialog 下的默认黑色暗淡背景.windowNoTitle 选项去掉上方的标题栏.

Setting windowIsFloating to false forces Dialog view to be expanded to full screen. Setting windowBackground to transparent removes default black dim background under Dialog. windowNoTitle option gets rid of the upper title bar.

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

Apply the theme and construct your custom_dialog view as follows.

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 视图是全屏视图,请将根布局的 background 设置为您喜欢的任何颜色.

Now that CustomDialog view is a full-screen view, set background of your root layout to whatever color you'd like.

我将结果镶嵌了一点.

这篇关于如何更改默认的黑色暗淡背景“颜色"?(不是dim的量)Dialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆