【问题标题】:Why is my default alert dialog button text white?为什么我的默认警报对话框按钮文本是白色的?
【发布时间】:2020-08-14 11:58:49
【问题描述】:

我的应用程序设置如下:

build.gradle

implementation 'com.google.android.material:material:1.1.0'

styles.xml

    <style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
        <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    </style>

    <style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:color">@color/colorPrimary</item>
    </style>

    <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:color">@color/colorPrimary</item>
    </style>

CheckoutFragment.kt

   private fun createConfirmOrderDialog() {
       val builder = AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme)
       builder.setTitle(getString(R.string.confirm_order))
           .setMessage(dialogPrompt)
           .setPositiveButton(R.string.confirm) { dialog, _ ->
               viewModel.placeOrder()
               dialog.dismiss()
           }
           .setNegativeButton(R.string.cancel) { dialog, _ ->
               dialog.dismiss()
           }
       builder.show()
  }

colors.xml

&lt;color name="colorAccent"&gt;#F1CB1A&lt;/color&gt; // I have this added to the base theme

但是,此设置显示了一个对话框,其中按钮文本不可见,因为文本和背景都是白色的。

我该如何解决这个问题?

【问题讨论】:

  • 我认为您调用了错误的 setPositiveButton 重载。试试.setPositiveButton(getString(R.string.confirm))。否定情况也是如此。
  • 不是真的,按钮和文本都存在,如果我将主题更改为 Appcompat 我可以看到它们。

标签: android android-alertdialog android-theme material-components-android android-ktx


【解决方案1】:

使用 MaterialAlertDialogBuilder 代替 AlertDialog.Builder

    MaterialAlertDialogBuilder(context)
        .setTitle("Title")
        .setMessage(dialogPrompt)
        .setPositiveButton("OK",listener)
        .show()

按钮的默认颜色基于colorPrimary 颜色。

如果您想使用自定义颜色,您可以使用:

    MaterialAlertDialogBuilder(context,R.style.AlertDialogTheme)

这种风格

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/.....</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/....</item>
</style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多