3
    dateLI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
                CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
                builder.setTheme(R.style.DateRangePickerTheme);
                builder.setCalendarConstraints(constraintsBuilder.build());
                MaterialDatePicker  picker = builder.build();
                assert getFragmentManager() != null;
                picker.show(getFragmentManager(), picker.toString());

            }
        });

我想要这个输出:

在此处输入图像描述

我从上面的代码中得到这个输出: 在此处输入图像描述

使用绿色勾号解决方案后,我得到此输出 在此处输入图像描述

我提到了日期选择器的风格:

    <style name="DateRangePickerTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">

        <item name="materialCalendarStyle">@style/Custom_MaterialCalendar.Fullscreen</item>
        <item name="android:orientation">horizontal</item>
        
    </style>


     <style name="Custom_MaterialCalendar.Fullscreen" parent="@style/Widget.MaterialComponents.MaterialCalendar.Fullscreen">
        <item name="android:windowFullscreen">false</item>
        
    </style>
4

3 回答 3

3

由于您使用的是Bridge主题,因此您必须在应用主题中添加这些属性:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
    <!-- ...... -->
    <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>
</style>

然后您可以在 with 中应用主题覆盖MaterialDatePicker

builder.setTheme(R.style.CustomThemeOverlay_MaterialCalendar_Fullscreen)

在哪里:

<style name="CustomThemeOverlay_MaterialCalendar_Fullscreen"
    parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen">
    <item name="materialCalendarStyle">@style/Custom_MaterialCalendar.Fullscreen</item>
</style>

<style name="Custom_MaterialCalendar.Fullscreen"
    parent="@style/Widget.MaterialComponents.MaterialCalendar.Fullscreen">
    <item name="android:windowFullscreen">false</item>
</style>

在此处输入图像描述

颜色基于您的应用主题中定义的colorPrimary, colorOnPrimary, 。colorOnPrimary您可以在以下内容中覆盖主题CustomThemeOverlay_MaterialCalendar_Fullscreen

<style name="CustomThemeOverlay_MaterialCalendar_Fullscreen"
    parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen">
    <item name="materialCalendarStyle">@style/Custom_MaterialCalendar.Fullscreen</item>
    <item name="colorPrimary">@color/...</item>
    <item name="colorOnSurface">@color/...</item>
</style>

在此处输入图像描述

于 2020-08-18T10:58:41.100 回答
1

实际上你不必使用这条线:

builder.setTheme(R.style.CustomThemeOverlay_MaterialCalendar_Fullscreen)

你可以这样设置你的应用主题:

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorSecondary">@color/colorSecondary</item>

    <!-- Picker styles and themes. -->
    <item name="materialCalendarStyle">@style/Widget.MaterialComponents.MaterialCalendar</item>
    <item name="materialCalendarFullscreenTheme">@style/CustomThemeOverlay_MaterialCalendar_Fullscreen</item>
    <item name="materialCalendarTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar</item>
</style>

然后以这种方式设置 CustomThemeOverlay_MaterialCalendar_Fullscreen:

    <style name="CustomThemeOverlay_MaterialCalendar_Fullscreen"
    parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen">
    <item name="colorPrimary">@color/yourcolor</item>
    <item name="colorOnSurface">@color/yourcolor</item>
</style>

就是这样。构建器将自动设置为应用程序的主题,其中日历主题设置为设计的主题。setTheme仅当我在应用程序中使用不同的日历(具有不同的主题)时,我才会使用。

于 2020-09-13T10:43:49.827 回答
0
 `<style name="DateRangePickerTheme"parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
    <item name="materialCalendarFullscreenTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen</item></style>`

尝试这个....

于 2020-08-18T12:16:59.223 回答