How can I change default black dim background “col

2019-01-14 13:20发布

enter image description here

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

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.

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.

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>

5条回答
叼着烟拽天下
2楼-- · 2019-01-14 13:49

try the code,

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



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

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

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

nb: the line d.setView(checkBoxView, 0, 0, 0, 0); will make it...

查看更多
乱世女痞
3楼-- · 2019-01-14 13:53

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.

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>

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.

CustomDialog.java

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

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

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

Sample result

I mosaiced the result a bit.

Result

查看更多
Summer. ? 凉城
4楼-- · 2019-01-14 13:59

This worked for me:

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) is the key.

查看更多
乱世女痞
5楼-- · 2019-01-14 14:03

use custom style.

<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:control the black dim background

查看更多
We Are One
6楼-- · 2019-01-14 14:11

Try to set the style for your dialog windows,

Example:

Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
查看更多
登录 后发表回答