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 22:02

For anyone using a custom dialog with a custom class you need to change the transparency in the class add this line in the onCreate():

getWindow().setBackgroundDrawableResource(android.R.color.transparent);
查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 22:04

I've faced the simpler problem and the solution i came up with was applying a transparent bachground THEME. Write these lines in your styles

    <item name="android:windowBackground">@drawable/blue_searchbuttonpopupbackground</item>
</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 then add

android:theme="@style/Theme.Transparent"

in your main manifest file , inside the block of the dialog activity.

Plus in your dialog activity XML set

 android:background= "#00000000"
查看更多
与风俱净
4楼-- · 2018-12-31 22:08

Dialog pop up fill default black background color or theme color so you need to set TRANSPARENT background into Dialog. Try below 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.splash);
dialog.show();
查看更多
浪荡孟婆
5楼-- · 2018-12-31 22:09

Try this in your code:

getWindow().setBackgroundDrawableResource(android.R.color.transparent);

it will definately working...in my case...! my frend

查看更多
倾城一夜雪
6楼-- · 2018-12-31 22:12
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

this is my way, you can try!

查看更多
人气声优
7楼-- · 2018-12-31 22:12
<style name="NewDialog">
    <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:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">@android:color/transparent</item>
</style>

use in java

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

I hope help you !

查看更多
登录 后发表回答