2

我有以下代码:

private void openDatePicker() {
    MaterialDatePicker picker =
            MaterialDatePicker.Builder.datePicker()
                    .setTitleText("Select date")
                    .setTheme(R.style.DatePickerTheme)
                    .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
                    .build();
    picker.show(getParentFragmentManager(),"tag");
    picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
        @Override
        public void onPositiveButtonClick(Object selection) {
            try {
                mtnRecDate.setText(sdf.format(selection));
            } catch (Exception e) {
                FancyToast.makeText(getContext(),"Setting Date failed!",FancyToast.LENGTH_LONG,FancyToast.ERROR,false).show();
            }

        }
    });
}

风格:

<!-- Picker styles and themes. -->
<style name="DatePickerTheme" parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar">
    <item name="materialCalendarStyle">@style/Widget.MaterialComponents.MaterialCalendar</item>
    <item name="materialCalendarFullscreenTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen</item>
    <item name="materialCalendarTheme">@style/Widget.MaterialComponents.MaterialCalendar.MonthNavigationButton</item>
    <item name="materialCalendarMonthNavigationButton">@style/Widget.MaterialComponents.MaterialCalendar.MonthNavigationButton</item>
</style>

我试图隐藏年份选项:

错误的

它应该看起来更像这样:

真的

我怎样才能做到这一点。我尝试更改样式主题(见上文),但没有任何结果。

4

1 回答 1

0

您可以查看文档。

正如下面提到的谷歌文档代码可以满足您的意图:

<style name="Theme.App" parent="Theme.MaterialComponents.*">
...
<item name="materialCalendarTheme">@style/ThemeOverlay.App.DatePicker</item>
</style>

<style name="ThemeOverlay.App.DatePicker" 
parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar">
<item name="colorPrimary">@color/shrine_pink_100</item>
<item name="colorOnPrimary">@color/shrine_pink_900</item>
<item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.App.SmallComponent</item>
<item name="shapeAppearanceMediumComponent">@style/ShapeAppearance.App.MediumComponent</item>
<!-- Customize text field of the text input mode. -->
<item name="textInputStyle">@style/Widget.App.TextInputLayout</item>

因此,您应该将样式从 MaterialCalendar 更改为 DatePicker。

于 2021-12-12T22:15:27.563 回答