自定义对话框中如何去除黑色背景

50

我想要移除自定义对话框上显示的黑色背景。我确定这个黑色背景来自于对话框,而不是应用程序的背景。

带黑色背景的自定义对话框

AlertDialog 代码:

public class MyAlertDialog extends AlertDialog { 
    public MyAlertDialog(Context context) 
    {  
        super(context); 
    }  

    public MyAlertDialog(Context context, int theme) 
    { super(context, theme); }
}

活动代码

public void showMyDialogOK(Context context, String s, DialogInterface.OnClickListener OkListener) {        
    MyAlertDialog myDialog = new MyAlertDialog(context, R.style.MyDialog2);        
    myDialog.setTitle(null); 
    myDialog.setMessage(s);        
    myDialog.setButton(DialogInterface.BUTTON_POSITIVE ,"Ok", OkListener);
    myDialog.show();    
}

样式

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="MyTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
            <item name="android:alertDialogStyle">@style/AlertDialog</item>  
        </style>    

        <style name="MyTheme2" parent="@android:style/Theme.Black">
            <item name="android:alertDialogStyle">@style/AlertDialog</item>    
        </style> 

        <style name="AlertDialog">        
            <item name="android:fullDark">@null</item>
            <item name="android:fullBright">@null</item>
            <item name="android:topDark">@drawable/popup_top_dark</item>
            <item name="android:topBright">@null</item>
            <item name="android:centerBright">@null</item>
            <item name="android:centerDark">@drawable/popup_center_dark</item>
            <item name="android:centerMedium">@null</item>
            <item name="android:bottomDark">@null</item>
            <item name="android:bottomBright">@null</item>
            <item name="android:bottomMedium">@drawable/popup_bottom_medium</item>
        </style>

        <style name="MyDialog2" parent="@android:Theme.Dialog">        
            <item name="android:windowBackground">@null</item>    
            <item name="android:buttonStyle">@style/CustomButton</item>  
        </style>    

        <style name="CustomButton" parent="@android:style/Widget.Button">        
            <item name="android:background">@drawable/button_stateful</item>  
        </style>
</resources>

图像资源

popup_center_dark.9.png

popup_center_dark.9.png

popup_bottom_medium.9.png

popup_bottom_medium.9.png

popup_top_dark.9.png

popup_top_dark.9.png


你以前也问过这个关于编程的问题是吧?https://dev59.com/JFvUa4cB1Zd3GeqPt3u0 - dira
1
这两个问题不同,第一个问题已经解决了之前的问题,但是带来了新的问题。之前的问题只想要去除边框而不是背景。 - pengwang
嗨,彭旺,你能分享一下自定义对话框的代码或者解释一下如何改变对话框的背景和按钮样式吗? - Shruti
没有一个答案对我有用。这里发布的答案是正确的解决方案。https://dev59.com/nF8e5IYBdhLWcg3w-OUn#25174316 - Tarun
15个回答

104
public MyAlertDialog(
        Context context, 
        int theme
    ) extends AlertDialog { 

    super(context, theme);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}

20
更简单的写法:getWindow().setBackgroundDrawableResource(android.R.color.transparent); - Brais Gabin
它可以在Android 5.0及以上版本上运行,但无法在Android 4.2上运行。 - LiangWang

27

一些方法getWindow().setBackgroundDrawable()使用AlertDialog时对我无效。我找到了一个更简单的解决方案,使用Dialog。以下是我的代码 -

        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(R.layout.popup_window);
        dialog.show();

25

试一下这个:

myDialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);

很棒!对我来说,其他答案中的解决方案没有使用这个标志是不起作用的。 - MSilva

10

在尝试了数十个其他解决方案后,最终适用于我的是:

<style name="translucentDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

然后将我的对话框设置为使用这个主题。


6

要删除背景的不透明度颜色,只需设置DimAmount即可

dialog.getWindow().setDimAmount(float amount);

新的dim数值,从0表示无调暗到1表示完全调暗。

5
请使用以下两行代码。已测试。 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

1

//在style.xml中的代码样式:

<style name="translucentDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

在活动中:将样式设置为对话框:
   Dialog dialogconf = new Dialog(TreeAct.this, R.style.translucentDialog);
            dialogconf.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                   dialogconf.setContentView(R.layout.dialog_layout);

1
你可以创建如下的 XML 布局并将其设置为对话框(dialog.xml)上的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" style="@style/white_background_bl_aatharv">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true" android:id="@+id/instructions_view">

        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textColor="#FFFFFF"
            android:text="text here " />
    </LinearLayout>
</ScrollView>

以下是设置警告对话框布局的代码:

AlertDialog alert = cndtnsbuilder.create();
alert.setView(LayoutInflater.from(
currentactivity.this).inflate(
R.layout.dialog, null));
alert.show();

是的,我知道这些方法,但我想使用修改系统样式的方法。 - pengwang
我认为我的方法没有错误,但是我不知道为什么会出现黑色背景。 - pengwang

1

只需更改对话框的父级元素。

带有黑色背景

<style name="MyDialog2" parent="@android:Theme.Dialog">        

没有黑色背景
<style name="MyDialog2" parent="@android:style/Theme.DeviceDefault.Light.Dialog">

1

以下方法对我有效:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接