Dialog with transparent background in Android

2018-12-31 21:21发布

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

enter image description here

final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger); 

16条回答
牵手、夕阳
2楼-- · 2018-12-31 21:58

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.

查看更多
旧时光的记忆
3楼-- · 2018-12-31 21:58

Add this code

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

Edit

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
查看更多
不流泪的眼
4楼-- · 2018-12-31 21:59

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

查看更多
查无此人
5楼-- · 2018-12-31 21:59

Same solution as zGnep but using xml:

android:background="@null"
查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 21:59

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();
查看更多
浪荡孟婆
7楼-- · 2018-12-31 22:01

You can use the:

setBackgroundDrawable(null);

method.And following is the doc:

  /**
    * Set the background to a given Drawable, or remove the background. If the
    * background has padding, this View's padding is set to the background's
    * padding. However, when a background is removed, this View's padding isn't
    * touched. If setting the padding is desired, please use
    * {@link #setPadding(int, int, int, int)}.
    *
    * @param d The Drawable to use as the background, or null to remove the
    *        background
    */
查看更多
登录 后发表回答