如何从Android对话框中删除黑色背景。这张图片显示了问题所在。
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
如何从Android对话框中删除黑色背景。这张图片显示了问题所在。
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
当前回答
如果你扩展了DialogFrament类,你可以设置主题:
setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);
然后在styles.xml文件中创建自定义主题(参见@LongLv的参数回答)
不要忘记添加<item name="android:windowCloseOnTouchOutside">true</item>如果你想让用户在对话框外触摸时关闭对话框。
其他回答
不知何故,撒迦利亚的解决方案对我不起作用,所以我用下面的主题来解决这个问题…
<style name="DialogCustomTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
可以将此主题设置为对话框,如下所示
final Dialog dialog = new Dialog(this, R.style.DialogCustomTheme);
享受! !
对于任何使用自定义类的自定义对话框的人,你需要改变类中的透明度,在onCreate()中添加这一行:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
如果你使用Kotlin,这段代码可以帮助你:
Objects.requireNonNull(dialog.window)
?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
ColorDrawable drawable = new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent));
dialog.getWindow().setBackgroundDrawable(drawable);
添加以下代码
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
或者换成这个:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);