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

发布于 2024-12-15 15:00:34 字数 3060 浏览 2 评论 0 原文

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

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

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

I want to remove the black background on custom dialog as shown in the picture. I'm sure the black background was from the dialog, not from app's background.

custom dialog with black background around it ;

AlertDialog code

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

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

Activity code

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

Styles

<?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>

Image 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

古镇旧梦 2024-12-22 15:00:36

这对我有用。在 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
        }
    }
}

This works for me.in 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
        }
    }
}
夏尔 2024-12-22 15:00:35
public MyAlertDialog(
        Context context, 
        int theme
    ) extends AlertDialog { 

    super(context, theme);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
public MyAlertDialog(
        Context context, 
        int theme
    ) extends AlertDialog { 

    super(context, theme);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
謸气贵蔟 2024-12-22 15:00:35

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

Sonehow getWindow().setBackgroundDrawable() didn't work for me with AlertDialog. I found an easier solution using Dialog. Here's my code -

        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();
心如狂蝶 2024-12-22 15:00:35

试试这个:

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

Try this:

myDialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
绝情姑娘 2024-12-22 15:00:35

在尝试了针对此问题的数十种其他解决方案之后,最终对我有用的是:

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

然后设置我的对话框以使用此主题。

After trying dozens of other solutions for this problem, what ended up working for me is:

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

And then setting my dialog to use this theme.

九命猫 2024-12-22 15:00:35

去除背景不透明颜色,只需设置 DimAmount

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

新的暗淡量,从 0(无暗淡)到 1(全暗淡)。

Remove the background opacity color, you just need to set the DimAmount

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

The new dim amount, from 0 for no dim to 1 for full dim.

奢华的一滴泪 2024-12-22 15:00:35

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

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

Use below two code lines.
Tested as well.

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

说不完的你爱 2024-12-22 15:00:35

以下方法对我有用:

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

Following method worked for me:

getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
魂牵梦绕锁你心扉 2024-12-22 15:00:35

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

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

// 在 Activity 中:将样式设置为对话框:

   Dialog dialogconf = new Dialog(TreeAct.this, R.style.translucentDialog);
            dialogconf.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                   dialogconf.setContentView(R.layout.dialog_layout);

//code style in style.xml :

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

// in activity :set style to dialog :

   Dialog dialogconf = new Dialog(TreeAct.this, R.style.translucentDialog);
            dialogconf.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                   dialogconf.setContentView(R.layout.dialog_layout);
岛歌少女 2024-12-22 15:00:35

只需更改 Dialog 的父级即可。

有黑色背景

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

无黑色背景

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

Just change Dialog parent them.

With black backround

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

Without black background

<style name="MyDialog2" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
心作怪 2024-12-22 15:00:35

您可以创建如下所示的 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();

you can create xml layout like following and set that layout on dialog(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>

here is the code to set layout on alert dialog :

AlertDialog alert = cndtnsbuilder.create();
alert.setView(LayoutInflater.from(
currentactivity.this).inflate(
R.layout.dialog, null));
alert.show();
瞎闹 2024-12-22 15:00:35

在 KOTLIN 中将此应用于警报对话框对象

window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

In KOTLIN apply this on alert dialog object

window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
巾帼英雄 2024-12-22 15:00:35

要去除背景色,在布局时,只需将背景设置为@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">

To remove the background color, on layout, you just need to set the background to @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">
树深时见影 2024-12-22 15:00:35
 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
撩动你心 2024-12-22 15:00:35

我的基于 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/

在 Oreo 8.1 上测试

I had the same problem with my custom dialog based on the Alertdialog.Builder, which had a black background showing in the title and the body when i use:

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

solution was
1- Use one of the pre-defines alert dialog builder's themes:

  • THEME_DEVICE_DEFAULT_DARK
  • THEME_DEVICE_DEFAULT_LIGHT
  • THEME_HOLO_DARK
  • THEME_HOLO_LIGHT THEME_TRADITIONAL

THEME_DEVICE_DEFAULT_LIGHT worked best for me

2 - set the default dialog button (positive / negative) colors to which ever color you desire like so:

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

check the below blog post for more detail and tricks to theming options:
http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

tested on Oreo 8.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文