Android Alert Dialog with extra background

2019-01-27 23:13发布

问题:

I recently migrated my app to Material Design and I stumbled upon this problem with my Alert Dialogs:

I'm applying the dialog style like this:

<item name="android:alertDialogTheme">@style/Theme.AlertDialog</item>

and Theme.AlertDialog looks like this:

<style name="Theme.AlertDialog" parent="Base.V14.Theme.AppCompat.Dialog">
    <item name="colorPrimary">@color/theme_primary</item>
    <item name="colorPrimaryDark">@color/theme_primary_dark</item>
    <item name="colorAccent">@color/theme_accent_dark</item>
</style>

This is happening on my Kitkat device and it works fine on Lollipop. Can you help me with getting rid of that outer background?

回答1:

The point is here:

<style name="Theme.AlertDialog" parent="Base.V14.Theme.AppCompat.Dialog">
    ...
    <item name="colorPrimary">@color/theme_primary</item>
    <item name="colorPrimaryDark">@color/theme_primary_dark</item>
    <item name="colorAccent">@color/theme_accent_dark</item>
    ...
    <item name="android:windowBackground">@android:color/transparent</item>
    ...
</style>


回答2:

As ironman told me here, be sure that you import the right class.

Right : import android.support.v7.app.AlertDialog;

Wrong : import android.app.AlertDialog;



回答3:

Use the theme in parent

AlertDialog.THEME_DEVICE_DEFAULT_LIGHT


回答4:

Add below styles . You have to customise background also.

    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">@android:color/transparent</item>

Using below also works

<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>

Also You can set in your code by using

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

and this should be before setContentView

dialog.setContentView(R.layout.dialog);


回答5:

I had the exact same symptom but for me it was actually that I had used the standard frameworks AlertDialog (and its Builder) instead of android.support.v7.app.AlertDialog, switching to use the one from the support library fixed the issue for me.