8

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

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

  • 在活动的清单集中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)
4

4 回答 4

8
    <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();
于 2013-07-10T19:37:31.600 回答
5

Use Dialog instead of AlertDialog.Builder and so use setContentView instead of setView.

于 2013-07-15T03:12:16.647 回答
4
connectionDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
于 2014-12-01T14:29:31.570 回答
0

看起来它与您的风格有关android:backgroundDimEnabled和/或android:backgroundDimAmount与您的风格有关。查看此答案以获取更多信息:Translucent Activity 填充整个屏幕

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

于 2013-07-10T19:54:10.387 回答