如何从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);
当前回答
你可以使用:
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
*/
其他回答
在我的例子中,没有什么可以应用透明背景。
只有我在我的对话onStart()中使用:
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
但这并没有起任何作用。我检查了styles.xml,没有任何与我的问题相关的内容。
最后,当我检查如何创建对话框时,我发现每当我请求对话框片段时,导航组件都会创建对话框。
在XML的navgraph.xml中,我将对话片段定义为片段,因此,它被创建为片段而不是对话框。将这个片段转换成对话框,一切就都到位了。
BTW:你不能在导航编辑器的GUI中从片段修改到对话框。你应该在代码中手工修改。
这可能是对话框的某些效果在运行时没有反映出来的原因之一。
不知何故,撒迦利亚的解决方案对我不起作用,所以我用下面的主题来解决这个问题…
<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);
享受! !
TL,博士;你只需要两件事,首先在你的风格做一些像这样的事情:
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
其次,100%确保上述样式应用到对话框中(可能通过传递给构造函数)。
完整的示例
<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>
在Java中使用:
Dialog dialog = new Dialog(this, R.style.NewDialog);
希望对你有所帮助!
确保r.b ayout.themechanger没有背景色,因为默认情况下对话框有默认背景色。
你还需要添加dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));
最后
<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>
您可以使用(可选)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
我建议创建一个扩展函数。类似的 extensions.kt
import android.app.Dialog
fun Dialog.setTransparentBackground() {
window?.setBackgroundDrawableResource(android.R.color.transparent)
}
并在任何对话中使用它
dialog.setTransparentBackground()
做一些有趣的编程……