Remove black background on custom dialog

前端 未结 13 2264
清歌不尽
清歌不尽 2021-01-30 20:54

I want to remove the black background on custom dialog as shown in the picture. I\'m sure the black background was from the dialog, not from app\'s background.

相关标签:
13条回答
  • 2021-01-30 21:17

    Sonehow getWindow().setBackgroundDrawable() didn't work for me with AlertDialog. I found an easier solution using Dialog. Here's my code -

            final Dialog dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.setContentView(R.layout.popup_window);
            dialog.show();
    
    0 讨论(0)
  • 2021-01-30 21:17

    Use below two code lines. Tested as well.

    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    0 讨论(0)
  • 2021-01-30 21:18

    Remove the background opacity color, you just need to set the DimAmount

    dialog.getWindow().setDimAmount(float amount);
    

    The new dim amount, from 0 for no dim to 1 for full dim.

    0 讨论(0)
  • 2021-01-30 21:19

    Try this:

    myDialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
    
    0 讨论(0)
  • 2021-01-30 21:20

    I had the same problem with my custom dialog based on the Alertdialog.Builder, which had a black background showing in the title and the body when i use:

    builder.setView(rootView)
      .setTitle(dialog_title)
      .setMessage(dialog_mesg)
    

    solution was 1- Use one of the pre-defines alert dialog builder's themes:

    • THEME_DEVICE_DEFAULT_DARK
    • THEME_DEVICE_DEFAULT_LIGHT
    • THEME_HOLO_DARK
    • THEME_HOLO_LIGHT THEME_TRADITIONAL

    THEME_DEVICE_DEFAULT_LIGHT worked best for me

    2 - set the default dialog button (positive / negative) colors to which ever color you desire like so:

     Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
     Button d = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
     b.setTextColor(ContextCompat.getColor(getActivity(), R.color.primary));
     d.setTextColor(ContextCompat.getColor(getActivity(), R.color.primary));
    

    check the below blog post for more detail and tricks to theming options: http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

    tested on Oreo 8.1

    0 讨论(0)
  • 2021-01-30 21:21

    After trying dozens of other solutions for this problem, what ended up working for me is:

    <style name="translucentDialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    

    And then setting my dialog to use this theme.

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