如何更改 Material 日期选择器的对话框按钮颜色

How to change the dialog's button colors for a Material Date picker

我正在使用 Material 组件来创建 DatePicker。据我了解,colorPrimary 用于确定对话框顶部部分的颜色,以及按钮上文本的颜色,特别是 CANCELOK .

通过文档,我似乎找不到改变只是底部按钮颜色的方法?可以吗?

当前代码:

<style name="ThemeOverlay.App.DatePicker" parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorOnPrimary">@android:color/white</item>
    <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.App.SmallComponent</item>
    <item name="shapeAppearanceMediumComponent">@style/ShapeAppearance.App.MediumComponent
    </item>
    <item name="materialCalendarHeaderTitle">@style/MyHeaderTitle</item>
    <item name="colorOnPrimarySurface">@android:color/white</item>
</style>

<style name="MyHeaderTitle" parent="Widget.MaterialComponents.MaterialCalendar.HeaderTitle">
    <item name="android:textColor">@android:color/white</item>
</style>

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

<style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="android:textSize">24sp</item>
    <item name="cornerSize">16dp</item>
</style>

<style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="android:textSize">30sp</item>
    <item name="cornerSize">16dp</item>
</style>

您可以使用:

builder.setTheme(R.style.MaterialCalendarTheme)

然后定义:

  <style name="MaterialCalendarTheme" parent="ThemeOverlay.MaterialComponents.MaterialCalendar">

    <!-- Buttons -->
    <item name="buttonBarPositiveButtonStyle">@style/TextButton</item>
    <item name="buttonBarNegativeButtonStyle">@style/TextButton</item>
  </style>
  <style name="TextButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/...</item>
    <item name="backgroundTint">@color/...</item>
  </style>