Android材料设计日期范围选择器 - 如何仅更改所选范围日期的文本颜色?

3

我正在尝试使用Material Range Date Picker重现此样式:

[日历日期选择器,所有选定的日期的文本颜色为白色。未选定的日期的文本颜色为黑色]2

目前我已经实现了以下效果:

[日历日期选择器,仅有所选边界日期的文本颜色为白色。未选定的日期的文本颜色为黑色,所选范围内的日期文本颜色也为黑色]3

  <style name="MyMaterialCalendarTheme" parent="ThemeOverlay.MaterialComponents.MaterialCalendar">
    <item name="colorOnSurface">@color/black</item>
    <item name="materialCalendarStyle">@style/MyMaterialCalendarStyle</item>
  </style>

  <style name="MyMaterialCalendarStyle"  parent="Widget.MaterialComponents.MaterialCalendar">
    <item name="rangeFillColor">@color/dark_green</item>
    <item name="daySelectedStyle">@style/MyDaySelectedStyle</item>
  </style>

  <style name="MyDaySelectedStyle" parent="Widget.MaterialComponents.MaterialCalendar.Day.Selected">
        <item name="itemTextColor">@color/white</item>
  </style>

显然,colorOnSurface 属性设置日历中所有日期的文本颜色,而 daySelectedStyle 属性仅为起始和结束日期对应的日期设置文本颜色。

是否有一种方法可以将起始日期和结束日期之间的日期文本颜色设置为白色,同时保持未选中日期的文本颜色为黑色?

1个回答

2

没有直接的方法可以实现这一点。但是你可以编辑源代码来达到这个目的。

daySelectedStyle属性只为最终日期和初始日期对设置textColor。

如果您查看RangeDateSelector的源代码,您会发现getSelectedDays()方法只会添加开始和结束日期。

RangeDateSelector

您可以将逻辑更改为包含两个选择日期之间的所有日期。
例如,您可以在两个if语句之间添加此代码:
if (selectedStartItem != null && selectedEndItem != null) {
  Long l = selectedStartItem + 86401000;
  for (; l < selectedEndItem;) {
    selections.add(l);
    l = l + 86401000;
  }
}

这个函数将会长这样:

VScode

颜色部分在MonthAdapter.java中执行。
最终结果:

ScreenShot


86401000是一天的毫秒数的近似值(Long),对吧?非常聪明的解决方案,@DrHowdyDoo!非常感谢!+1 - Mirian Fonkam

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