如何从Android对话框中删除黑色背景。这张图片显示了问题所在。

final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger); 

当前回答

在我的例子中,没有什么可以应用透明背景。

只有我在我的对话onStart()中使用:

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

但这并没有起任何作用。我检查了styles.xml,没有任何与我的问题相关的内容。

最后,当我检查如何创建对话框时,我发现每当我请求对话框片段时,导航组件都会创建对话框。

在XML的navgraph.xml中,我将对话片段定义为片段,因此,它被创建为片段而不是对话框。将这个片段转换成对话框,一切就都到位了。

BTW:你不能在导航编辑器的GUI中从片段修改到对话框。你应该在代码中手工修改。

这可能是对话框的某些效果在运行时没有反映出来的原因之一。

其他回答

添加以下代码

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

或者换成这个:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

这就是我用AlertDialog实现半透明的方法。

创建自定义样式:

<style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
    <item name="android:colorBackground">#32FFFFFF</item>
</style>

然后创建对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
AlertDialog dialog = builder.create();

Kotlin创建透明背景对话框的方法:

 Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
            .apply {
                // requestWindowFeature(Window.FEATURE_NO_TITLE)
                setCancelable(true)
                setContentView(R.layout.define_your_custom_view_id_here)

                //access your custom view buttons/editText like below.z
                val createBt = findViewById<TextView>(R.id.clipboard_create_project)
                val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
                val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
                val manualOption =
                    findViewById<TextView>(R.id.clipboard_manual_add_project_option)

                //if you want to perform any operation on the button do like this

                createBt.setOnClickListener {
                    //handle your button click here
                    val enteredData = clipboard_et.text.toString()
                    if (enteredData.isEmpty()) {
                        Utils.toast("Enter project details")
                    } else {
                        navigateToAddProject(enteredData, true)
                        dismiss()
                    }
                }

                cancelBt.setOnClickListener {
                    dismiss()
                }
                manualOption.setOnClickListener {
                    navigateToAddProject("", false)
                    dismiss()
                }
                show()
            }

在style.xml中创建LoadingIndicatorDialogStyle,如下所示:

<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/black_transperant</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:background">@android:color/transparent</item>
    <!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->
</style>

你可以使用:

setBackgroundDrawable(null);

方法。下面是文档:

  /**
    * Set the background to a given Drawable, or remove the background. If the
    * background has padding, this View's padding is set to the background's
    * padding. However, when a background is removed, this View's padding isn't
    * touched. If setting the padding is desired, please use
    * {@link #setPadding(int, int, int, int)}.
    *
    * @param d The Drawable to use as the background, or null to remove the
    *        background
    */

我遇到过更简单的问题,我想出的解决方案是应用透明的背景主题。按照你的风格写出这些行

    <item name="android:windowBackground">@drawable/blue_searchbuttonpopupbackground</item>
</style>
<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

然后加上

android:theme="@style/Theme.Transparent"

在主清单文件中,在对话框活动的块内。

在您的对话框活动XML集

 android:background= "#00000000"