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

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个回答

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

如果您能描述问题的来源以及为什么这个解决方案会起作用,那将更好。 - Dev-iL

0
在 Kotlin 中,将此应用于警报对话框对象。
window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

0

要移除背景颜色,在布局上,只需要将背景设置为@null即可。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@null">

0

我在使用AlertDialog.Builder创建自定义对话框时,遇到了同样的问题:标题和正文区域出现了黑色背景,解决方法如下:

builder.setView(rootView)
  .setTitle(dialog_title)
  .setMessage(dialog_mesg)

解决方案是:

  • 使用预定义的警报对话框构建器主题之一:
  • THEME_DEVICE_DEFAULT_DARK
  • THEME_DEVICE_DEFAULT_LIGHT
  • THEME_HOLO_DARK
  • THEME_HOLO_LIGHT THEME_TRADITIONAL

对我来说,THEME_DEVICE_DEFAULT_LIGHT 最好用。

2 - 将默认对话框按钮(正/负)颜色设置为您想要的任何颜色,如下所示:

 Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
 Button d = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
 b.setTextColor(ContextCompat.getColor(getActivity(), R.color.primary));
 d.setTextColor(ContextCompat.getColor(getActivity(), R.color.primary));

查看以下博客文章以获取更多详细信息和主题选项技巧: http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

在Oreo 8.1上进行了测试


0
这对我来说很有效,在bottomSheetDialogFragment中。
    override fun onStart() {
    super.onStart()
    // remove black outer overlay, or change opacity
    dialog?.window?.also { window ->
        window.attributes?.also { attributes ->
            attributes.dimAmount = 0.1f
            window.attributes = attributes
        }
    }
}

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