Android 为什么我的默认警报对话框按钮文本为白色?

Android 为什么我的默认警报对话框按钮文本为白色?,android,android-alertdialog,android-theme,material-components-android,android-ktx,Android,Android Alertdialog,Android Theme,Material Components Android,Android Ktx,我已按如下方式设置我的应用程序: build.gradle implementation'com.google.android.material:material:1.1.0' styles.xml <style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert"> <item name="button

我已按如下方式设置我的应用程序:

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>
colors.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>
#F1CB1A//我已将此添加到基本主题中

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


如何修复此问题?

使用
材质AlertDialogBuilder
而不是
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>

@样式/负片按钮样式
@样式/正按钮样式
@颜色/。。。。。
@颜色/。。。。

使用
材质AlertDialog Builder
而不是
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>

@样式/负片按钮样式
@样式/正按钮样式
@颜色/。。。。。
@颜色/。。。。

我认为您调用了
setPositiveButton
的错误重载。使用
.setPositiveButton(getString(R.string.confirm))
尝试。消极情况也是如此。实际上,按钮和文本不存在,如果我将主题更改为Appcompat,我可以看到它们。我认为您调用的
setPositiveButton
重载错误。使用
.setPositiveButton(getString(R.string.confirm))
尝试。同样,对于否定的情况,按钮和文本并不存在,如果我将主题更改为Appcompat,我可以看到它们。