随着Android 的材质组件 https://github.com/material-components/material-components-android/blob/master/docs/components/Picker.md你可以使用新的MaterialDatePicker https://github.com/material-components/material-components-android/blob/master/docs/components/Picker.md.
为了正常工作,在您的应用程序中您必须使用材料成分主题 https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md#4-change-your-app-theme-to-inherit-from-a-material-components-theme.
通过这种方式,您可以继承选择器的样式和主题。
要选择一个单一日期只需使用:
MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText(R.string.your_text);
MaterialDatePicker<Long> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
要选择一个范围日期您可以使用日期范围选择器:
MaterialDatePicker.Builder<Pair<Long, Long>> builder =
MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());
MaterialDatePicker<?> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
检查颜色在你的主题中使用。
这些属性定义了您的风格。你不需要要添加它们,它们默认随 Material Components 主题一起提供。
<!-- Picker styles and themes. -->
<item name="materialCalendarStyle">@style/Widget.MaterialComponents.MaterialCalendar</item>
<item name="materialCalendarFullscreenTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen</item>
<item name="materialCalendarTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar</item>
基于这些样式,选择器使用的颜色是:
HeaderLaoyout -> background:colorPrimary, textColor:colorOnPrimary
HeaderSelection -> background:colorPrimary, textColor:colorOnPrimary
ConfirmButtons -> background:colorPrimary, textColor:colorOnPrimary
Buttons -> background:colorPrimary, textColor:colorOnSurface
HeaderToggleButton-> textColor:colorOnPrimary
Day -> text:colorOnSurface stroke:colorOnSurface
SelectedDay -> background:colorPrimary, textColor:colorOnPrimary
RangeFillColor -> background:colorPrimary