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:

dialog appears to be having two backgrounds and I don't know how to get rid of the outer background

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?

5条回答
家丑人穷心不美
2楼-- · 2019-01-27 23:53

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-27 23:55

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>
查看更多
趁早两清
4楼-- · 2019-01-28 00:04

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;

查看更多
再贱就再见
5楼-- · 2019-01-28 00:06

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);
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-28 00:16

Use the theme in parent

AlertDialog.THEME_DEVICE_DEFAULT_LIGHT
查看更多
登录 后发表回答