[英]Dialog with transparent background in Android
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);
Add this code添加此代码
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Or this one instead:或者这个:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
TL;DR; TL;博士; You just need two things, firstly in your style
do something like:您只需要两件事,首先在您的style
中执行以下操作:
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
Secondly, make 100% sure said style
gets applied to your dialog (maybe by passing to constructor).其次,确保 100% 确定将上述style
应用于您的对话框(可能通过传递给构造函数)。
<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 helps you !希望对你有帮助!
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加上您的对话活动 XML 集中
android:background= "#00000000"
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!!享受!!
如果你想破坏对话框的深色背景,使用这个
dialog.getWindow().setDimAmount(0);
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
*/
Dialog pop up fill default black background color or theme color so you need to set TRANSPARENT
background into Dialog.对话框弹出填充默认黑色背景颜色或主题颜色,因此您需要将TRANSPARENT
背景设置为对话框。 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();
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.这是因为它们都用纯色覆盖了负责边距的android:windowBackground
属性。 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 SDK 中进行了一些挖掘,发现默认的窗口背景可绘制,并对其进行了一些修改以允许透明对话框。
First, copy /platforms/android-22/data/res/drawable/dialog_background_material.xml to your project.首先,将 /platforms/android-22/data/res/drawable/dialog_background_material.xml 复制到您的项目中。 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
.请注意, android:color
设置为?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
.要让自定义样式中定义在android:background
中的颜色透明并显示透明度,我们要做的就是将?attr/colorBackground
更改为@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.确保将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!然后像往常一样构建、创建和显示对话框!
Attention : Don't use builder for changing 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);
dialog.show();
When using Dialog.builder, it's not giving getWindow()
option in it.使用 Dialog.builder 时,它没有在其中提供getWindow()
选项。
Try this in your code:在你的代码中试试这个:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
it will definately working...in my case...!它肯定会工作......在我的情况下......! my frend我的朋友
与 zGnep 相同的解决方案,但使用 xml:
android:background="@null"
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();
You can use(optional)您可以使用(可选)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
I would recommend creating an extension function .我建议创建一个扩展函数。 Something like extensions.kt
像extensions.kt
这样的东西
import android.app.Dialog
fun Dialog.setTransparentBackground() {
window?.setBackgroundDrawableResource(android.R.color.transparent)
}
and use it on any dialog by并在任何对话框中使用它
dialog.setTransparentBackground()
Have some fun programming...有一些有趣的编程...
use this code it's works with me :使用此代码,它适用于我:
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();
In my case solution works like this:在我的情况下,解决方案是这样的:
dialog_AssignTag.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
And Additionally in Xml of custom dialog:此外,在自定义对话框的 XML 中:
android:alpha="0.8"
In case you extended the DialogFrament
class, you can set the theme with:如果您扩展了DialogFrament
类,您可以使用以下方式设置主题:
setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);
And then make the custom theme in your styles.xml file (see @LongLv's answer for parameters)然后在您的 styles.xml 文件中创建自定义主题(请参阅@LongLv 的参数答案)
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.如果您希望在用户触摸对话框外部时关闭对话框,请不要忘记添加<item name="android:windowCloseOnTouchOutside">true</item>
。
Set these style code in style在 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只需将 false 更改为 true 即可
<item name="android:backgroundDimEnabled">true</item>
It will dim your background.它会使你的背景变暗。
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
this is my way, you can try!这是我的方法,你可以试试!
对于使用带有自定义类的自定义对话框的任何人,您需要更改类中的透明度,在 onCreate() 中添加以下行:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent)));
Make sure R.layout.themechanger
has no background color because by default the dialog has a default background color.确保R.layout.themechanger
没有背景颜色,因为默认情况下对话框具有默认背景颜色。
You also need to add dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));
您还需要添加dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));
And finally最后
<style name="TransparentDialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
</style>
If you use Kotlin , this code can help you:如果您使用Kotlin ,此代码可以帮助您:
Objects.requireNonNull(dialog.window)
?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
In my case, nothing was working to apply a transparent background.就我而言,没有任何方法可以应用透明背景。
Only I used in my dialog onStart():只有我在我的对话框 onStart() 中使用过:
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
But it was not taking any effect.但它没有任何效果。 I checked styles.xml, nothing linked to my problem.我检查了styles.xml,没有与我的问题相关联。
Finally, when I was checking how my dialog is gets created I found that the navigation component was creating the dialog whenever I request the dialog fragment.最后,当我检查我的对话框是如何创建的时,我发现导航组件在我请求对话框片段时正在创建对话框。
There In XML of navgraph.xml, I defined the dialog fragment as a fragment so, It was created as a fragment instead of a dialog.在 navgraph.xml 的 XML 中,我将对话框片段定义为片段,因此,它被创建为片段而不是对话框。 Changing that fragment to dialog made everything fall in place.将该片段更改为对话框使一切都到位。
BTW: You cannot modify from fragment to dialog in the GUI of the navigation editor.顺便说一句:您不能在导航编辑器的 GUI 中从片段修改为对话框。 You should change by hand in code.您应该手动更改代码。
This might be one of a cause when some effect on a dialog is not reflected in runtime.这可能是对对话框的某些影响未反映在运行时的原因之一。
In Kotlin you can use this code:在 Kotlin 你可以使用这个代码:
var dialog=Dialog(context)
dialog.window!!.decorView.background=null
Kotlin way to create dialog with transparent background: Kotlin 创建具有透明背景的对话框的方法:
Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
.apply {
// requestWindowFeature(Window.FEATURE_NO_TITLE)
setCancelable(true)
setContentView(R.layout.define_your_custom_view_id_here)
//access your custom view buttons/editText like below.z
val createBt = findViewById<TextView>(R.id.clipboard_create_project)
val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
val manualOption =
findViewById<TextView>(R.id.clipboard_manual_add_project_option)
//if you want to perform any operation on the button do like this
createBt.setOnClickListener {
//handle your button click here
val enteredData = clipboard_et.text.toString()
if (enteredData.isEmpty()) {
Utils.toast("Enter project details")
} else {
navigateToAddProject(enteredData, true)
dismiss()
}
}
cancelBt.setOnClickListener {
dismiss()
}
manualOption.setOnClickListener {
navigateToAddProject("", false)
dismiss()
}
show()
}
Create LoadingIndicatorDialogStyle in style.xml like this:在 style.xml 中创建 LoadingIndicatorDialogStyle,如下所示:
<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<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:statusBarColor">@color/black_transperant</item>
<item name="android:layout_gravity">center</item>
<item name="android:background">@android:color/transparent</item>
<!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.