MaterialDatePicker 错误:要在您的应用主题中设置 materialCalendarFullscreenTheme 属性

Bik*_*ram 7 android datepicker material-components-android

我做了什么:

  1. 添加 implementation 'com.google.android.material:material:1.1.0'在依赖项中
  2. 设置Theme.MaterialComponents.Light.Bridge为应用主题的父级
     <style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
Run Code Online (Sandbox Code Playgroud)
  1. 试图在片段中显示日期选择器。
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();

        Calendar now = Calendar.getInstance();
        now.set(Calendar.YEAR, 2020);
        now.set(Calendar.MONTH, 1);
        now.set(Calendar.DAY_OF_MONTH, 10);

        long first = now.getTimeInMillis();

        now.set(Calendar.YEAR, 2020);
        now.set(Calendar.MONTH, 5);
        now.set(Calendar.DAY_OF_MONTH, 20);

        long last = now.getTimeInMillis();

        builder.setSelection(new Pair<>(first, last));

        MaterialDatePicker<Pair<Long, Long>> picker = builder.build();

        picker.show(fragmentActivity.getSupportFragmentManager(), "RangePicker");
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,出现此错误

java.lang.IllegalArgumentException: com.google.android.material.datepicker.MaterialDatePicker
requires a value for the com.example:attr/materialCalendarFullscreenTheme attribute to be set
in your app theme. You can either set the attribute in your theme or update your theme to
inherit from Theme.MaterialComponents (or a descendant).
Run Code Online (Sandbox Code Playgroud)

Bik*_*ram 17

只需将以下内容添加到您的应用主题即可。

<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>
Run Code Online (Sandbox Code Playgroud)

添加后

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

   <!-- Add these -->
   <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>
Run Code Online (Sandbox Code Playgroud)

  • 或者,您应该迁移到不带“Bridge”后缀的新“Theme.MaterialComponents.*”主题,该后缀应自动包含所需的主题和样式。 (3认同)
  • 使用上述任一主题都会使我的选项卡栏失去其效果,并且未选定的选项卡文本不可见。不好。相反,从 Theme.MaterialComponents.DayNight.DarkActionBar.Bridge 扩展您的 AppTheme,或者使用 light 而不是 DayNight... (2认同)