【问题标题】:android AlertDialog with transparent background具有透明背景的android AlertDialog
【发布时间】:2013-07-10 19:10:10
【问题描述】:

我有一个自定义的 AlertDialog,我想让它的背景完全透明。 通常要使活动完全透明,我会执行以下操作

  • 在 xml 布局中将背景设置为 #00000000

  • 在活动的清单集android:theme="@android:style/Theme.Holo.Dialog" 中。

  • 在 onCreate 添加getWindow().setBackgroundDrawable(new ColorDrawable(0))

    但是现在我正在处理一个对话框,我该如何实现透明度?

这是对话框代码:

LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.activity_mine1,
        (ViewGroup) findViewById(R.layout.mine1));
    mine1 = new AlertDialog.Builder(this);
    mine1.setView(dialoglayout);
    mine1.show();

而我的 xml 只是一个带有其他子视图的 relativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000" >

    ...

</RelativeLayout>

注意:我已经在这里查看了一些类似的帖子,但它们似乎不起作用。

我真正的原因是我真正想要使用的背景不是矩形。我让它在一项活动中工作。但我想改用对话框。

编辑:

继续玩,我有这个style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomDialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
</resources>

我添加为

new AlertDialog.Builder(this, R.style.CustomDialog)

【问题讨论】:

  • attr dialogBackground 未被 eclipse 识别。

标签: android android-layout transparency android-alertdialog


【解决方案1】:
    <style name="CustomAlertDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:width">300dip</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

            Dialog connectionDialog = new Dialog(this, R.style.CustomAlertDialog);
            connectionDialog.setContentView(set your view here);
            connectionDialog.show();

【讨论】:

  • 你的成就不亚于我自己的style.xml。背景几乎不透明;天很黑;您必须非常努力地发现存在一些薄弱的透明度。我正在寻找#00000000。此外,由于某种原因,背景周围有一个白色边框。
  • 宽度属性 300dip 没有生效。
【解决方案2】:

使用 Dialog 代替 AlertDialog.Builder,因此使用 setContentView 代替 setView。

【讨论】:

    【解决方案3】:
    connectionDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    

    【讨论】:

      【解决方案4】:

      看起来它与您的风格中的 android:backgroundDimEnabled 和/或 android:backgroundDimAmount 有关。请查看此答案以获取更多信息:Translucent Activity filling the entire screen

      您可能想尝试将android:backgroundDimEnabled设置为false。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 2011-07-13
        相关资源
        最近更新 更多