删除自定义对话框上的黑色背景

Posted

技术标签:

【中文标题】删除自定义对话框上的黑色背景【英文标题】:Remove black background on custom dialog 【发布时间】:2011-12-28 09:43:58 【问题描述】:

我想,如图所示。我确定黑色背景来自对话框,而不是来自应用程序的背景。

;

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_bottom_medium.9.png

popup_top_dark.9.png

【问题讨论】:

你以前也问过这个问题吧? ***.com/questions/7497449/… 这两个问题不一样,问题已经解决了,解决了之前的问题,但是带来了新的问题。之前的问题只想去除框架而不是背景 hi pengwang,能否分享一下自定义对话框的代码或者解释一下你是如何改变对话框按钮样式的背景 没有一个答案对我有用。此处发布的答案是正确的解决方案。 ***.com/a/25174316/786337 【参考方案1】:
public MyAlertDialog(
        Context context, 
        int theme
    ) extends AlertDialog  

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

【讨论】:

更简单:getWindow().setBackgroundDrawableResource(android.R.color.transparent); 它适用于Android 5.0 +,但不适用于Android 4.2【参考方案2】:

Sonehow 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();

【讨论】:

【参考方案3】:

试试这个:

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

【讨论】:

太棒了!对我来说,没有这个标志,其他答案中的解决方案就无法工作。【参考方案4】:

在为这个问题尝试了几十种其他解决方案之后,最终对我有用的是:

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

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

【讨论】:

【参考方案5】:

去掉背景不透明度,只需要设置DimAmount

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

新的暗淡量,从 0 表示没有暗淡到 1 表示完全暗淡。

【讨论】:

【参考方案6】:

使用以下两行代码。 也经过测试。

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

【讨论】:

【参考方案7】:

以下方法对我有用:

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

【讨论】:

【参考方案8】:

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

【讨论】:

【参考方案9】:

只需更改 Dialog 的父级即可。

黑色背景

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

没有黑色背景

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

【讨论】:

【参考方案10】:

你可以像下面这样创建xml布局并在对话框(dialog.xml)上设置布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_ android:layout_
    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_
        android:layout_ android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true" android:id="@+id/instructions_view">

        <TextView android:id="@+id/TextView01" android:layout_
            android:layout_ 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();

【讨论】:

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

要去除背景色,在布局时,只需将背景设置为@null

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

【讨论】:

【参考方案12】:
 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

【讨论】:

如果您还可以描述问题的根源以及为什么该解决方案应该有效,那就更好了。【参考方案13】:

我基于 Alertdialog.Builder 的自定义对话框遇到了同样的问题,当我使用该对话框时,标题和正文中显示黑色背景

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

解决方案是 1- 使用预定义警报对话框构建器的主题之一:

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/

在奥利奥 8.1 上测试

【讨论】:

以上是关于删除自定义对话框上的黑色背景的主要内容,如果未能解决你的问题,请参考以下文章

黑色背景白色自定义过渡

iOS 8 自定义 UIActivity 图片黑色背景色

自定义PNG翻转功能在图像上输出黑色背景

如何在android的linearlayout中删除阴影?

去除安卓自定义Dialog黑色背景,设置无边框,透明

GTK3 主题对话框在 Audacity 中呈现纯黑色背景