How to set Custom Dialog background dim?

痞子三分冷 提交于 2020-03-25 18:00:31

问题


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

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>

And Style Codes are this.

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

These are dimen values

<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


回答1:


You can try this.

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

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



回答2:


Me help this

WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
/** set the dim amount of the settings activity */
lp.dimAmount = 0.5f;
dialog.getWindow().setAttributes(lp);



回答3:


try this it's worked

dialog.getWindow().setBackgroundDrawable(new 
ColorDrawable(android.graphics.Color.TRANSPARENT));


来源:https://stackoverflow.com/questions/60404653/how-to-set-custom-dialog-background-dim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!