如何设置自定义对话框背景暗淡? [英] How to set Custom Dialog background dim?

查看:102
本文介绍了如何设置自定义对话框背景暗淡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了自定义对话框,但不会使背景颜色变暗. 我尝试了这段代码,它显示为黑色背景.

I made custom dialog and it doesn't make background color dim. I tried this code and it shows with black background.

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

也尝试过此方法,但结果相同.背景仍然是黑色的.

Also tried this, but it shows same result. Background is still black.

 window.setDimAmount(0.5f);

这是我的CustomDialog布局代码

This is my CustomDialog layout Code

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview"
    android:background=""
    style="@style/PopupView"
    android:elevation="5dp"
    card_view:cardCornerRadius="@dimen/card_band_radius">


    <RelativeLayout android:orientation="vertical"
        android:background="@drawable/twobtndialog_bg"
        android:layout_width="wrap_content"
        android:layout_height="160dp">


        <TextView
            android:id="@+id/tv_message"
            android:layout_marginTop="40dp"
            android:layout_centerHorizontal="true"
            android:textColor="@color/dark"
            android:fontFamily="@font/nanumsquareregular"
            android:textSize="16sp"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <LinearLayout
            android:id="@+id/ll_btn"
            android:orientation="horizontal"
            android:layout_marginTop="40dp"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/btn_left"
                android:clickable="true"
                android:src="@drawable/btn_no"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <ImageView
                android:id="@+id/btn_right"
                android:clickable="true"
                android:src="@drawable/btn_ok"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>


        </LinearLayout>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

样式代码就是这个.

    <style name="PopupView" parent="CardView">
        <item name="android:layout_width">@dimen/popup_width</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:layout_margin">30dp</item>
        <item name="cardBackgroundColor">@color/paleGrey</item>
    </style>

这些是维度值

<resources>
    <dimen name="popup_title_height">46.5dp</dimen>
    <dimen name="popup_title_padding">5dp</dimen>
    <dimen name="card_band_radius">10dp</dimen>
    <dimen name="item_list_view_line_width">0.5dp</dimen>
    <dimen name="popup_width">300dp</dimen>
</resources>

我想让对话框显示如下图所示

I want to make my dialog shows like this image below

推荐答案

您可以尝试一下.

<style name="NewDialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:backgroundDimEnabled">true</item>  <!--Background clear-->
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">false</item>
</style>

重要点是backgroundDimEnabled

Important point is backgroundDimEnabled

dialog = new Dialog(activity, R.style.NewDialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

这篇关于如何设置自定义对话框背景暗淡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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