Android 透明的AlertDialog有黑色背景

Android 透明的AlertDialog有黑色背景,android,android-alertdialog,Android,Android Alertdialog,我有一个自定义的AlertDialog样式,使AlertDialog框透明。它工作正常,只是当我将所需的透明布局充气到“警报”对话框窗口时,它会以黑色背景显示。我的目标是拥有一个完全透明的AlertDialog,似乎只有4个按钮浮动,而不是一个框架。 图一是自定义对话框给我的,图二是我想要的或我的目标 这是自定义对话框的代码 <style name="CustomDialog" parent="android:Theme.Dialog">

我有一个自定义的
AlertDialog
样式,使
AlertDialog
框透明。它工作正常,只是当我将所需的透明布局充气到“警报”对话框窗口时,它会以黑色背景显示。我的目标是拥有一个完全透明的
AlertDialog
,似乎只有4个按钮浮动,而不是一个框架。 图一是自定义对话框给我的,图二是我想要的或我的目标

这是自定义
对话框的代码

<style name="CustomDialog" parent="android:Theme.Dialog">
    <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>
AlertDialog.Builder imageDialog = new AlertDialog.Builder(TimeLine.this, R.style.CustomDialog);
inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.tabs, null);
AlertDialog a = imageDialog.create();
a.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
a.setView(view, 0, 0, 0, 0);

a.show();

编辑* 选项卡布局xml的代码在这里 `


`


通过测试了解问题的根源,我发现布局确实是透明的,因为当我更改布局的背景色时,警报对话框也会更改。但是,当布局设置为“透明”时,充气布局后面似乎是黑色的。正因为如此,我不确定该怎么做,也不确定它是
AlertDialog
设置还是我的布局代码。

您是否尝试过使用类似的方法:

<style name="CustomDialog" parent="@android:style/Theme.Translucent">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    ...
</style>

R.layout.tabs

替换

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/tabLayout"
    android:background="#000000ff">


问题在于,
AlertDialog builder
实际上不适合设计透明对话框,并且始终具有黑色背景,这实际上是它的主题,请使用
对话框
来创建透明主题

示例:

Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();

使用
对话框
不需要对透明背景进行任何主题操作,因此基本上很简单。

那些在创建自定义透明对话框或警报对话框时仍有问题的人,可以使用此组合。我还想显示自定义背景这是什么工作为我

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
例如:-

/**
 * alert dialog
 */

    public class ToolTipView extends AlertDialog {

        public ToolTipView(@NonNull Context context) {
            super(context);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_tool_tip_view);
        }

    }

发布R.layout.tabsxml@Rod_Algonquin我添加了代码,结果与此风格相同。Java代码适合我。问题是棒棒糖的白色背景。美好的这也没有效果。的确,布局是透明的,但布局背后的背景是黑色的。谢谢!你的回答是我三天来试图解决这个问题的结束。太好了。@superuser我记得以前有过这个问题吗?:)这是所有人的正确答案。“使用警报对话框对我不起作用。@Rod_Algonquin如何为我们设置为ContentView的布局中的视图编写单击事件?谢谢。我被显示白色角卡住了,即使背景图像有透明角。谢谢兄弟。”。我想去掉暗淡的半暗背景,但找不到任何答案。getWindow().clearFlags函数删除了半透明部分。
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
/**
 * alert dialog
 */

    public class ToolTipView extends AlertDialog {

        public ToolTipView(@NonNull Context context) {
            super(context);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_tool_tip_view);
        }

    }