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

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

问题描述

(This是在互联网上显示对话框的随机图像。)

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

我一直在实现自定义对话框。我可以处理对话框中几乎所有的东西,除了对话框本身的默认黑色背景,但在它后面的整个屏幕上。基本上我想改变其颜色和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游荡,但只有我发现的答案是关于改变背景对话框本身。在任何情况下,如果你需要它,这里是我的简单代码。

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



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 force 对话框将视图扩展为全屏。将 windowBackground 设置为透明删除对话框下的默认黑色背景 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



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 屏幕视图,将您的根布局的背景设置为您想要的任何颜色。

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

我结果有点麻木了。

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

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