【问题标题】:Different Color Background in my AlertDialog in KotlinKotlin 的 AlertDialog 中的不同颜色背景
【发布时间】:2021-04-05 04:37:40
【问题描述】:

我的自定义警报对话框发生了一件非常奇怪的事情。相同的代码在不同的时间运行时,会产生不同的警报对话框布局的背景颜色,其中大多数时候颜色是红色(无处不在!),即使我对布局使用了不同的背景。先看看我的代码。

这是 AlertDialog 的布局(layout_add_url.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_margin="@dimen/_10sdp"
    android:id="@+id/url_layout_constraint"
    android:background="@drawable/background_note"
    android:layout_height="wrap_content">

//Some  ImageViews and TextViews here

这就是layout_add_url.xml终于有了

现在,

这是 background_note.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#333333"/>             //Note that the color here is black
    <corners android:radius="@dimen/_10sdp"/>
</shape>

这是我最终在 Kotlin Activity 中创建 AlertDialog 的地方

 val inflater = layoutInflater
            val view = inflater.inflate(R.layout.layout_add_url, null)
            val infoDialogBuilder = AlertDialog.Builder(this@CreateNotesActivity)
            infoDialogBuilder.setView(view)
            val infoDialog = infoDialogBuilder.create()
            infoDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            infoDialog.setContentView(view)
            infoDialog.show()

但是在真实设备上运行项目之后。看起来像这样

注意 我注意到,当我将 android:background="@drawable/background_note" 更改为 android:background="#333333" 时,代码似乎工作正常

【问题讨论】:

    标签: android kotlin popup window


    【解决方案1】:

    您可以尝试通过样式属性设置对话框的背景并使用MaterialAlertDialogBuilder

    layout_add_url 中的ConstraintLayout 中删除android:background="@drawable/background_note" 属性

    style.xml 中定义您的对话框样式:

    <style name="style_dialog" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
        <item name="shapeAppearanceMediumComponent">@style/style_dialog_background</item>
        <item name="colorSurface">#333333</item>
    </style>
    
    <style name="style_dialog_background" parent="ShapeAppearance.MaterialComponents.MediumComponent">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">@dimen/_10sdp</item>
    </style>
    

    然后通过调用创建您的对话框:

    val dialogView = layoutInflater.inflate(R.layout.layout_add_url, null)
    MaterialAlertDialogBuilder(this@CreateNotesActivity, R.style.style_dialog)
            .setView(dialogView)
            .create().apply {
                show()
            }
    

    【讨论】:

    • 感谢您的回复!我一定会试一试的。
    • 另外,我用另一个background_note1.xml(具有相同的元素)替换了我的background_note.xml,一切似乎都运行良好。 IDK这是怎么发生的,但现在一切都很好。我的猜测是,这可能是因为我打开对话框的活动与对话框本身具有相同的背景(background_note.xml)。但即使是这样,我仍然无法找出原因。
    • 我在我的代码上检查了你的解决方案,但它仍然不起作用。相同的红色盛行。 :(
    • 尝试从ConstraintLayout中删除android:background="@drawable/background_note"属性layout_add_url
    • 我这样做了,然后我的背景全是白色的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多