【问题标题】:How to set Custom Dialog background dim?如何设置自定义对话框背景暗淡?
【发布时间】:2020-02-25 23:28:52
【问题描述】:

我制作了自定义对话框,它不会使背景颜色变暗。 我试过这段代码,它以黑色背景显示。

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

也试过这个,但它显示相同的结果。背景仍然是黑色的。

 window.setDimAmount(0.5f);

这是我的自定义对话框布局代码

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

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

【问题讨论】:

    标签: android android-layout kotlin dialog


    【解决方案1】:

    你可以试试这个。

    <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

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

    【讨论】:

      【解决方案2】:

      帮帮我

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

      【讨论】:

        【解决方案3】:

        试试这个效果很好

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-10
          • 1970-01-01
          • 2015-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-20
          相关资源
          最近更新 更多