【问题标题】:Transparent AlertDialog has black background透明的 AlertDialog 有黑色背景
【发布时间】:2015-07-04 13:00:36
【问题描述】:

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

这是自定义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();

编辑* 标签布局 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 设置还是我的布局代码。

【问题讨论】:

  • 发布 R.layout.tabs xml
  • @Rod_Algonquin 我已添加代码

标签: android android-alertdialog


【解决方案1】:

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

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

    }

【讨论】:

  • 谢谢兄弟。我想去除昏暗的半暗背景,但找不到任何答案。 getWindow().clearFlags 函数去掉了半透明部分。
【解决方案2】:

您是否尝试过使用类似的东西:

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

编辑

在java代码中试试这个

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

【讨论】:

  • 这种风格的结果是一样的。
  • Java 代码为我工作。问题是棒棒糖上的白色背景。不错!
【解决方案3】:

在你的R.layout.tabs

替换

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

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/tabLayout"
    android:background="@android:color/transparent">

【讨论】:

  • 这也没有效果。确实布局是透明的,但是布局背后的背景是黑色的。
【解决方案4】:

问题是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 不需要对透明背景进行任何主题操作,因此基本上很容易。

【讨论】:

  • 谢谢!您的回答是我尝试解决此问题的 3 天的结束。完美。
  • @superuserdo 我记得以前有过这个问题。 :))
  • 这是所有正确答案。使用警报对话框对我不起作用。
  • @Rod_Algonquin 如何为我们设置为 ContentView 的布局中的视图编写点击事件?
  • 谢谢..我被显示的白色角落卡住了,即使背景图像有透明的角落。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
相关资源
最近更新 更多