【问题标题】:How can I change default black dim background "color" (not the amount of dim) of Dialog?如何更改对话框的默认黑色暗淡背景“颜色”(而不是暗淡量)?
【发布时间】:2015-05-27 10:32:30
【问题描述】:

(这是在互联网上找到的显示Dialog的随机图像。)

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

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

CustomDialog.java

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>

【问题讨论】:

  • 试试 dialog.getWindow().setBackgroundDrawableResource(R.drawable.yourdrawable);
  • @Harry 这并没有解决我的问题,因为我的custom_dialog.xml 中已经有background,其效果完全相同。
  • @yushi 那篇文章处理相同的方面,但有没有办法改变颜色,而不是暗淡的数量?
  • @LeeHanKyeol 我不这么认为。

标签: android dialog


【解决方案1】:

使用自定义样式。

<style name="transparent_dialog_borderless" parent="android:Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">#FF333333</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

android:backgroundDimEnabled:控制黑色暗淡背景

【讨论】:

  • 我试过background,它改变了Dialog的“内部”背景。我试过windowBackground,它改变了Dialog的“外部”背景,它只在Dialog的大小范围内改变颜色。你能告诉我处理backgroundDim颜色的项目名称吗?
【解决方案2】:

试试代码,

View checkBoxView = View.inflate(context, R.layout.alertbox, null);



    final AlertDialog.Builder builder = new AlertDialog.Builder(context);

             builder.setView(checkBoxView);
             builder.setCancelable(false);
                Dialog d = builder.create();

                d.getWindow().setBackgroundDrawable(new ColorDrawable(0));
                d.setView(checkBoxView, 0, 0, 0, 0);
                d.show();

nb:行 d.setView(checkBoxView, 0, 0, 0, 0);会成功的...

【讨论】:

  • 答案中的d 是什么?
【解决方案3】:

尝试为您的对话窗口设置样式,

例子:

Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);

【讨论】:

    【解决方案4】:

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

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

    styles.xml

    <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 选项去掉了上方的标题栏。

    CustomDialog.java

    如下应用主题并构建您的custom_dialog 视图。

    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 设置为您想要的任何颜色。

    样本结果

    我将结果镶嵌了一点。

    【讨论】:

      【解决方案5】:

      这对我有用:

      val dialog = AlertDialog.Builder(context)
                      .setView(view)
                      .setCancelable(true)
                      .setPositiveButton(R.string.done_label, { dialog, _ -> dialog.dismiss() })
                      .create()
              dialog.window.setDimAmount(0f)
              dialog.show()
      

      dialog.window.setDimAmount(0f) 是关键。

      【讨论】:

        【解决方案6】:

        下面的自定义 DatePickerDoalog 类不仅可以自定义暗淡颜色,还可以让暗淡呈现动画效果

        /**
         * @author Taras Yurkiv @Devlight
         */
        public class DatePickerDialogCustomDim extends DatePickerDialog {
        
            private final long animDuration = 100;
            private float dimAmount = 0.7f;
        
            private Drawable dimDrawable;
            private ViewGroup root;
        
            private OnDismissListener outsideDismissListener;
        
            private final OnDismissListener dismissListener = new OnDismissListener() {
        
                @Override
                public void onDismiss(DialogInterface dialog) {
                    final ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                            PropertyValuesHolder.ofInt("alpha", (int) (255 * dimAmount), 0));
                    animator.setTarget(dimDrawable);
                    animator.setDuration(animDuration);
                    animator.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            ViewGroupOverlay overlay = root.getOverlay();
                            overlay.remove(dimDrawable);
                        }
                    });
                    animator.start();
                    if (outsideDismissListener != null)
                        outsideDismissListener.onDismiss(dialog);
                }
            };
        
        
            @TargetApi(Build.VERSION_CODES.N)
            public DatePickerDialogCustomDim(@NonNull Context context) {
                this(context, 0);
            }
        
            @TargetApi(Build.VERSION_CODES.N)
            public DatePickerDialogCustomDim(@NonNull Context context, @StyleRes int themeResId) {
                this(context, themeResId, null, -1, -1, -1);
                init(context);
            }
        
            public DatePickerDialogCustomDim(@NonNull Context context,
                                             @Nullable OnDateSetListener listener,
                                             int year,
                                             int month,
                                             int dayOfMonth) {
                this(context, 0, listener, year, month, dayOfMonth);
            }
        
            public DatePickerDialogCustomDim(@NonNull Context context,
                                             @StyleRes int themeResId,
                                             @Nullable OnDateSetListener listener,
                                             int year,
                                             int monthOfYear,
                                             int dayOfMonth) {
                super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
                init(context);
            }
        
            private void init(Context context) {
                root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
                super.setOnDismissListener(dismissListener);
            }
        
            public void setDimAmount(@FloatRange(from = 0, to = 1f) float dim) {
                dimAmount = dim;
            }
        
            @Override
            public void show() {
                super.show();
                dimDrawable = new ColorDrawable(Color.WHITE); // a dim color
                dimDrawable.setBounds(0, 0, root.getWidth(), root.getHeight());
        
                ViewGroupOverlay overlay = root.getOverlay();
                overlay.add(dimDrawable);
        
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                        PropertyValuesHolder.ofInt("alpha", 0, (int) (255 * dimAmount)));
                animator.setTarget(dimDrawable);
                animator.setDuration(animDuration);
                animator.start();
            }
        
            @Override
            public void setOnDismissListener(@Nullable OnDismissListener listener) {
                outsideDismissListener = listener;
            }
        }
        

        它与样式结合使用

        <style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
            <item name="colorAccent">@color/accent</item>
            <item name="android:textColorLink">@color/primary</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:backgroundDimEnabled">false</item>
        </style>
        

        【讨论】:

          【解决方案7】:

          为 backgroundDimAmount 设置一个介于 0-1 之间的浮点值(它是一个浮点值)

              <style name="DarkTransparentBgDialog" parent="@android:style/Theme.Dialog">
                 <item name="android:backgroundDimEnabled">true</item>
                 <item name="android:backgroundDimAmount">0.87</item>
              </style>
          

          在 DialogFragment 中(在 Kotlin 中)

          override fun getTheme() = R.style.DarkTransparentBgDialog
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-09-10
            • 1970-01-01
            • 2013-04-20
            • 1970-01-01
            • 1970-01-01
            • 2017-09-19
            • 1970-01-01
            • 2014-03-13
            相关资源
            最近更新 更多