Android中具有透明背景的对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10795078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dialog with transparent background in Android
提问by Programmer
How do I remove the black background from a dialog box in Android. The pic shows the problem.
如何从 Android 的对话框中删除黑色背景。图片显示了问题。
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
回答by Zacharias Manuel
Add this code
添加此代码
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Or this one instead:
或者这个:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
回答by LongLv
<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
在java中使用
Dialog dialog = new Dialog(this, R.style.NewDialog);
I hope help you !
希望能帮到你!
回答by Fahad Ishaque
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
我遇到了更简单的问题,我想出的解决方案是应用透明的 bachground 主题。用你的风格写下这些行
<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
加上你的对话活动 XML 集中
android:background= "#00000000"
回答by Ravi K. Sharma
Somehow Zacharias solution didn't work for me so I have used the below theme to resolve this issue...
不知何故,Zacharias 解决方案对我不起作用,所以我使用下面的主题来解决这个问题......
<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!!
享受!!
回答by zionpi
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
*/
回答by duggu
Dialog pop up fill default black background color or theme color so you need to set TRANSPARENT
background into Dialog. Try below code:-
对话框弹出填充默认的黑色背景颜色或主题颜色,因此您需要将TRANSPARENT
背景设置为对话框。试试下面的代码:-
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();
回答by erfan
if you want destroy dark background of dialog , use this
如果你想破坏对话框的深色背景,使用这个
dialog.getWindow().setDimAmount(0);
回答by IvonLiu
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.
我在所有现有答案中发现的一个问题是没有保留边距。这是因为它们都android:windowBackground
用纯色覆盖了负责边距的属性。但是,我在 Android SDK 中进行了一些挖掘,发现默认窗口背景可绘制,并对其进行了一些修改以允许透明对话框。
First, copy /platforms/android-22/data/res/drawable/dialog_background_material.xml to your project. Or, just copy these lines into a new file:
首先,将 /platforms/android-22/data/res/drawable/dialog_background_material.xml 复制到您的项目中。或者,只需将这些行复制到一个新文件中:
<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:
请注意,android:color
设置为?attr/colorBackground
。这是您看到的默认纯灰色/白色。为了让android:background
自定义样式中定义的颜色透明并显示透明度,我们要做的就是更改?attr/colorBackground
为@android:color/transparent
。现在它看起来像这样:
<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.
确保替换newly_created_background_name
为您刚创建的可绘制文件的实际名称,并替换some_transparent_color
为所需的透明背景。
After that all we need to do is set the theme. Use this when creating the AlertDialog.Builder
:
之后,我们需要做的就是设置主题。创建时使用它AlertDialog.Builder
:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);
Then just build, create, and show the dialog as usual!
然后像往常一样构建、创建和显示对话框!
回答by Dileep P G
This is what I did to achieve translucency with AlertDialog.
这就是我使用 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();
回答by jigar
Try this in your code:
在你的代码中试试这个:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
it will definately working...in my case...! my frend
它肯定会工作......在我的情况下......!我的朋友