【发布时间】:2021-10-04 15:17:33
【问题描述】:
我正在使用此代码应用具有透明背景的颜色。
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(MyColor));
它在我的手机上运行得很好,但在我的平板电脑上它不工作
在我的对话框上显示具有白色和灰色背景(我使用自定义视图和背景设置为@null):
【问题讨论】:
我正在使用此代码应用具有透明背景的颜色。
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(MyColor));
它在我的手机上运行得很好,但在我的平板电脑上它不工作
在我的对话框上显示具有白色和灰色背景(我使用自定义视图和背景设置为@null):
【问题讨论】:
根据您的帖子,我有一些问题可能会导致这些透明度问题。
1.-由于您的平板电脑显示不同的渲染,可能是操作系统 API 低于您手机上的版本?如果是这种情况,请尝试使用与您的手机操作系统 API 匹配的适当支持库。
2.-假设两个设备在 OS API 上相似,您可以尝试两种不同的方法。
a) 将零作为 ColorDrawable 的参数传递 :)
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
b) 创建一个不同的活动只是为了显示您的对话框并隔离与您的父活动相关的主题(肯定会导致问题),执行类似的操作下面。
<style name="Transparent" parent="@android:style/Theme.NoTitleBar">
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
以上是您必须与对话活动相关联的主题,然后在您的 XML 布局上使用以下内容。
android:background="#aa000000"
您可以将此方法应用于显示演示屏幕或说明性所需的对话、活动:)
【讨论】:
你可以试试这个,不过好像已经有人建议了...?
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
相反,您是否尝试过类似的方法?
<style name="TransparentDialogTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
然后,当你制作对话时,试试这个......?
final Dialog dialog = new Dialog(this, R.style.TransparentDialogTheme);
这有点矫枉过正,但如果你真的需要它也可能需要它:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TransparentDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@color/orange_transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>
</resources>
【讨论】:
问题是 AlertDialog 构建器实际上不适合设计透明对话框,并且总是有黑色背景,这实际上是它的主题,而是使用 Dialog 来创建透明主题。
示例:
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
使用 Dialog 不需要对透明背景进行任何主题操作,因此基本上很容易。
【讨论】:
这对我有用:
alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
如果没有,请查看this post
【讨论】:
使用这个:
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
【讨论】:
您也需要禁用暗淡:
Window w = dialog.getWindow();
w.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
LayoutParams lp = w.getAttributes();
lp.flags &= ~LayoutParams.FLAG_DIM_BEHIND;
lp.dimAmount = 0;
w.setAttributes(lp);
【讨论】:
我建议只创建一个具有透明背景的活动,然后您可以根据您的要求在活动中显示您想要的任何内容并轻松自定义它。这里有一些东西可以帮助你开始
How do I create a transparent Activity on Android?
当我尝试在我的应用程序中实现类似的显示忙碌屏幕时,帮了我很多忙。我希望你觉得这对你有帮助,并让我知道你选择了哪条路。祝你好运。
【讨论】:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
这在 lolipop 下面的 api 和上面或 lolipop 的使用中都可以
android:background="@android:color/transparent"
在对话框片段的父布局中,因此简而言之,在所有 api 中使用两者来克服这个问题。
【讨论】:
先添加这样的样式
<style name="customStyleDialog">
<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">true</item>
<item name="android:background">@android:color/transparent</item>
</style>
之后,当您创建对话框时,您也可以按如下方式设置此样式。
Dialog dialog = new Dialog(getActivity(), R.style.customStyleDialog);
// other settings to the dialog
// for making work transparency on low level devices add next line too after show
// dialog
dialog.show();
dialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
确保在视图中添加背景色,通过该对话框设置为该对话框
.setContentView(--);
【讨论】:
在对话框中使用主题。
<style name="Dialog_Transparent" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
public class RProgressDg extends Dialog {
public RProgressDg(Context context) {
this(context, R.style.Dialog_Transparent);
}
public RProgressDg(final Context context, int theme) {
super(context, theme);
this.setContentView(R.layout.dg_pro_round);
}
}
【讨论】:
对于 Kotlin,您可以将此行更改为新行:
旧:
Objects.requireNonNull(dialog.window)?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
新:
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
【讨论】: