问题
How do I remove the black background from a dialog box in Android. The pic shows the problem.
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
回答1:
Add this code
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Or this one instead:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
回答2:
<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 !
回答3:
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:
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:
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
*/
回答6:
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();
回答7:
One issue I found with all the existing answers is that the margins aren't preserved. This is because they all override the android:windowBackground
attribute, which is responsible for margins, with a solid color. However, I did some digging in the Android SDK and found the default window background drawable, and modified it a bit to allow transparent dialogs.
First, copy /platforms/android-22/data/res/drawable/dialog_background_material.xml to your project. Or, just copy these lines into a new file:
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:inset="16dp">
<shape android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="?attr/colorBackground" />
</shape>
</inset>
Notice that android:color
is set to ?attr/colorBackground
. This is the default solid grey/white you see. To allow the color defined in android:background
in your custom style to be transparent and show the transparency, all we have to do is change ?attr/colorBackground
to @android:color/transparent
. Now it will look like this:
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:inset="16dp">
<shape android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@android:color/transparent" />
</shape>
</inset>
After that, go to your theme and add this:
<style name="MyTransparentDialog" parent="@android:style/Theme.Material.Dialog">
<item name="android:windowBackground">@drawable/newly_created_background_name</item>
<item name="android:background">@color/some_transparent_color</item>
</style>
Make sure to replace newly_created_background_name
with the actual name of the drawable file you just created, and replace some_transparent_color
with the desired transparent background.
After that all we need to do is set the theme. Use this when creating the AlertDialog.Builder
:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);
Then just build, create, and show the dialog as usual!
回答8:
Same solution as zGnep but using xml:
android:background="@null"
回答9:
Try this in your code:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
it will definately working...in my case...! my frend
回答10:
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();
回答11:
if you want destroy dark background of dialog use this
dialog.getWindow().setDimAmount(0);
回答12:
use this code it's works with me :
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();
回答13:
Attention :
don't use builder for change background
Dialog dialog = new Dialog.Builder(MainActivity.this)
.setView(view)
.create();
dialog.show();dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
change to
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
mDialog.show();
When using Dialog.builder - Its not giving getwindow() option in it.
回答14:
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.
回答15:
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
this is my way, you can try!
回答16:
In my case solution works like this:
dialog_AssignTag.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
And Additionally in Xml of custom dialog:
android:alpha="0.8"
回答17:
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.
回答18:
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);
来源:https://stackoverflow.com/questions/10795078/dialog-with-transparent-background-in-android