gpt4 book ai didi

android - 透明的 AlertDialog 有黑色背景

转载 作者:可可西里 更新时间:2023-11-01 19:08:35 50 4
gpt4 key购买 nike

我有一个自定义 AlertDialog 样式,它使 AlertDialog 框透明。它工作正常,除了当我将我想要的透明布局膨胀到警报对话框窗口时,它显示为黑色背景。我的目标是让一个完全透明的 AlertDialog 看起来好像只有 4 个按钮 float 而不是一个框架。图一是自定义对话框给我的,图二是我想要的或我的目标。

这是自定义Dialog

的代码
<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>

这是我在 onCreate()

中调用的内容
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();

current alert dialog

Desired AlertDialog

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

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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_marginTop="206dp"
android:layout_below="@+id/button4"
android:layout_alignStart="@+id/button4" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="100dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp" />
</RelativeLayout>

`

通过测试查看问题的根源,我发现布局确实是透明的,因为当我更改布局的背景颜色时,警告对话框也会更改。然而,当布局设置为透明时,似乎膨胀后的布局是黑色的。因此,我不确定该怎么做,或者是 AlertDialog 设置还是我的布局代码。

最佳答案

问题是 AlertDialog builder 实际上不适合设计透明对话框,并且总是有这个黑色背景,这实际上是它的主题,而不是使用 Dialog 改为创建透明主题。

示例:

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();

使用 Dialog 不需要对透明背景进行任何主题操作,因此基本上很容易。

关于android - 透明的 AlertDialog 有黑色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25174165/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com