Dialog with transparent background in Android

前端 未结 20 2400
孤城傲影
孤城傲影 2020-11-22 15:02

How do I remove the black background from a dialog box in Android. The pic shows the problem.

\"enter

相关标签:
20条回答
  • 2020-11-22 15:20

    Set these style code in style

    <style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
    

    And simply change false to true below line

    <item name="android:backgroundDimEnabled">true</item>
    

    It will dim your background.

    0 讨论(0)
  • 2020-11-22 15:21

    Add this code

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

    Or this one instead:

    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    
    0 讨论(0)
  • 2020-11-22 15:23

    Somehow Zacharias solution didn't work for me so I have used the below theme to resolve this issue...

    <style name="DialogCustomTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>
    

    One can set this theme to dialog as below

    final Dialog dialog = new Dialog(this, R.style.DialogCustomTheme); 
    

    Enjoy!!

    0 讨论(0)
  • 2020-11-22 15:23

    This is what I did to achieve translucency with AlertDialog.

    Created a custom style:

    <style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
        <item name="android:colorBackground">#32FFFFFF</item>
    </style>
    

    And then create the dialog with:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
    AlertDialog dialog = builder.create();
    
    0 讨论(0)
  • 2020-11-22 15:24

    Same solution as zGnep but using xml:

    android:background="@null"
    
    0 讨论(0)
  • 2020-11-22 15:26

    In case you extended the DialogFrament class, you can set the theme with:

    setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);
    

    And then make the custom theme in your styles.xml file (see @LongLv's answer for parameters)

    Don't forget to add <item name="android:windowCloseOnTouchOutside">true</item> if you want the dialog to close if the user touches outside the dialog.

    0 讨论(0)
提交回复
热议问题