在Android中使用透明背景的对话框
如何从Android中的对话框中删除黑色背景。 图片显示了问题。
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
16个解决方案
578 votes
添加此代码
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
编辑
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Zacharias Manuel answered 2019-03-08T18:15:24Z
71 votes
@null
@android:color/transparent
true
@null
@null
@android:style/Animation.Dialog
stateUnspecified|adjustPan
false
@android:color/transparent
在java中使用
Dialog dialog = new Dialog(this, R.style.NewDialog);
希望对你有所帮助!
LongLv answered 2019-03-08T18:16:00Z
26 votes
我遇到了一个更简单的问题,我想出的解决方案是应用透明的bachground主题。 在你的风格中写下这些线条
@drawable/blue_searchbuttonpopupbackground
true
@android:color/transparent
@null
true
true
false
然后添加
android:theme="@style/Theme.Transparent"
在主清单文件中,在对话框活动的块内。
另外还有对话框活动XML集
android:background= "#00000000"
Fahad Ishaque answered 2019-03-08T18:16:47Z
13 votes
不知何故Zacharias解决方案对我没有用,所以我用下面的主题来解决这个问题......
@android:color/transparent
@null
可以将此主题设置为对话框,如下所示
final Dialog dialog = new Dialog(this, R.style.DialogCustomTheme);
请享用!!
Ravi K. Sharma answered 2019-03-08T18:17:26Z
11 votes
你可以使用:
setBackgroundDrawable(null);
方法。以下是文档:
/**
* 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
*/
zionpi answered 2019-03-08T18:17:56Z
11 votes
对话框弹出填充默认的黑色背景颜色或主题颜色,因此您需要将TRANSPARENT背景设置为Dialog。 尝试以下代码: -
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();
duggu answered 2019-03-08T18:18:24Z
7 votes
我在所有现有答案中发现的一个问题是边距不会被保留。 这是因为它们都覆盖了AlertDialog.Builder属性,该属性负责边距,具有纯色。 但是,我在Android SDK中进行了一些挖掘,发现默认窗口背景是可绘制的,并对其进行了一些修改以允许透明对话框。
首先,将/platforms/android-22/data/res/drawable/dialog_background_material.xml复制到您的项目中。 或者,只需将这些行复制到新文件中:
android:inset="16dp">
请注意,AlertDialog.Builder设置为some_transparent_color.这是您看到的默认灰色/白色。 要使自定义样式中android:background中定义的颜色透明并显示透明度,我们所要做的就是将?attr/colorBackground更改为@android:color/transparent.现在它将如下所示:
android:inset="16dp">
之后,转到您的主题并添加:
@drawable/newly_created_background_name
@color/some_transparent_color
确保将AlertDialog.Builder替换为刚刚创建的可绘制文件的实际名称,并将some_transparent_color替换为所需的透明背景。
之后,我们需要做的就是设置主题。 在创建AlertDialog.Builder时使用此选项:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);
然后像往常一样构建,创建和显示对话框!
IvonLiu answered 2019-03-08T18:19:49Z
3 votes
与zGnep相同的解决方案,但使用xml:
android:background="@null"
XST answered 2019-03-08T18:20:16Z
3 votes
这就是我用AlertDialog实现半透明的方法。
创建自定义样式:
#32FFFFFF
然后使用以下命令创建对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
AlertDialog dialog = builder.create();
Dileep P G answered 2019-03-08T18:21:05Z
2 votes
在您的代码中尝试此操作:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
它肯定会工作......在我的情况下......! 我的朋友
jigar answered 2019-03-08T18:21:41Z
1 votes
使用此代码它适用于我:
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();
a.hammad answered 2019-03-08T18:22:11Z
1 votes
注意:
不要使用构建器来更改背景
Dialog dialog = new Dialog.Builder(MainActivity.this)
.setView(view)
.create();
dialog.show();dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
改成
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
mDialog.show();
使用Dialog.builder时 - 它没有给出getwindow()选项。
Rasoul Miri answered 2019-03-08T18:22:57Z
0 votes
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
这是我的方式,你可以尝试!
Hello Sun answered 2019-03-08T18:23:26Z
0 votes
在我的情况下解决方案是这样的:
dialog_AssignTag.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
另外在Xml的自定义对话框中:
android:alpha="0.8"
Sagar Shah answered 2019-03-08T18:24:12Z
0 votes
如果您扩展了true类,可以使用以下命令设置主题:
setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);
然后在styles.xml文件中创建自定义主题(参见@LongLv的参数答案)
如果您希望在用户触摸对话框之外关闭对话框,请不要忘记添加true。
B.Cakir answered 2019-03-08T18:25:05Z
0 votes
对于使用自定义对话框和自定义类的任何人,您需要更改类中的透明度,在onCreate()中添加此行:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Haider Malik answered 2019-03-08T18:25:42Z