我试图用 Material Range Date Picker 重现这种风格:
[带有白色文本颜色的所有选定日期的日历日期选择器。未选择的日子有黑色文本颜色]
这是我到目前为止能够实现的:
[日历日期选择器,只有选定的边界日期为白色文本颜色。未选定的日期具有黑色文本颜色,选定边界内的日期也为黑色]
<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
属性为日历中的所有日期设置了 textColor,而 daySelectedStyle
属性仅为最后一天和初始日期设置了 textColor。
有没有办法让开始日期和结束日期之间的天数的 textColor 变为白色,同时将未选中天数的文本颜色保持为黑色?
最佳答案
嗯,没有直接的方法可以做到这一点。但是您可以编辑源代码来实现这一点。
daySelectedStyle attribute sets the textColor only for the pair of final and initial days.
如果你看到RangeDateSelector的源代码,您可以看到 getSelectedDays()
方法只添加了开始和结束日期。
您可以更改逻辑以包含两个选定日期之间的所有日期。
例如,您可以在两个 if 语句之间添加以下代码:
if (selectedStartItem != null && selectedEndItem != null) {
Long l = selectedStartItem + 86401000;
for (; l < selectedEndItem;) {
selections.add(l);
l = l + 86401000;
}
}
函数看起来像这样:
着色部分在MonthAdapter.java中执行
最终结果:
关于Android Material Date Range Picker - 如何仅更改选定范围日期的文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73024658/