自定义对话框未显示,只有淡黑色背景。

3

好的,我一直在尝试编写一个单独的类来自定义对话框,并在这里使用静态函数,以下是代码:

class CustomDialog {
    companion object {
        fun create(context: Context, content: String) {
            context as Activity
            val inflater = context.layoutInflater
            val view = inflater.inflate(R.layout.dialog_info, null)
            val infoDialog = AlertDialog.Builder(context).create()
            view.dialog_content.text = content
            view.dialog_okButton.setOnClickListener {
                println("CLICKED")
                infoDialog.dismiss()
            }
            infoDialog.setContentView(view)

            infoDialog.show()
        }
    }
}

我要加载的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:background="@drawable/dialog_background"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp">

    <TextView
        android:id="@+id/dialog_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/iranyekanregular"
        android:text="Some text"
        android:textColor="@color/white" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="10dp"
        android:background="@color/white" />

    <Button
        android:id="@+id/dialog_okButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/iranyekanregular"
        android:gravity="center"
        android:clickable="true"
        android:focusable="true"
        android:text="OK" />
</LinearLayout>

一个简单的文本视图,带有一个分隔线和一个按钮用于关闭对话框。
但是,每当我触发按钮显示对话框时,它只会显示淡化的黑色屏幕,而不是对话框本身。 如果我使用`setView(view)`而不是`setContentView`,它基本上会自己显示出来,但由于它只是默认对话框的中间(内容)部分,所以我看到了默认对话框的背景,并且关闭按钮仍然无法工作。任何帮助将不胜感激。顺便说一句,我已经搜索了很多,但没有成功。

可能是你为你的类所设置的样式导致的。尝试给对话框一个不同的样式,然后检查是否有效。 - Umair
如果您在inflater.inflate(R.layout.dialog_info, null)中传递活动根布局而不是null,它可能会正常工作。 - akashzincle
@Umair,您能否详细说明一下?如果您所说的“样式”是指布局的背景,我已经更改为纯色,但没有成功。 - Arman Momeni
@ArmanMomeni 不好意思,我的意思是第二个答案建议您使用主题。我想说的是,请使用您自己的主题或者安卓提供的主题。 - Umair
2个回答

1
你忘记将充气的视图添加到对话框中。
class CustomDialog {
companion object {
    fun create(context: Context, content: String) {
        context as Activity
        val inflater = context.layoutInflater
        val view = inflater.inflate(R.layout.dialog_info, null)
        val infoDialogBuilder = AlertDialog.Builder(context)
        infoDialogBuilder.setView(view)
        val infoDialog = infoDialogBuilder.create()
        view.dialog_content.text = content
        view.dialog_okButton.setOnClickListener {
            println("CLICKED")
            infoDialog.dismiss()
        }
        infoDialog.setContentView(view)

        infoDialog.show()
    }
}

这是缺失的行:
infoDialogBuilder.setView(view)

0
问题出在你没有为它设置任何样式。
Use  infoDialog.setView(view)

在创建对话框时,使用材料主题样式。

val infoDialog = AlertDialog.Builder(ContextThemeWrapper(context,android.R.style.ThemeOverlay_Material_Dialog)).create()

最后

infoDialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
infoDialog.show()

你的代码其余部分都没问题。


好的,这个可行,不需要主题部分,因为那是不必要的。使用setView时,我得到了默认对话框的丑陋锐角背景,但是将背景设置为透明后,一切都消失了。我不知道是否使用setView的方式是正确的,因为默认对话框仍在显示,并且我有可用的积极、消极和中性选项,而我不应该有。如果有其他/更好的解决方案,我会再等待一段时间,否则你的解决方案已经解决了我的问题,我会标记它的。谢谢。 - Arman Momeni
好的,但是当您添加主题部分时,那些默认对话框的丑陋尖角背景也将被移除。如果您不设置它,Android 将设置默认样式。 - Khurram Shahzad

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