材料日期选择器错误:需要在应用程序主题中设置materialCalendarFullscreenTheme属性

14

我的做法:

  1. 在dependencies中添加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>
  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");

当我运行代码时,出现了这个错误

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).
2个回答

30

只需将以下内容添加到您的应用程序主题中。

<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 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>

5
你可以选择迁移到没有“Bridge”后缀的新的 Theme.MaterialComponents.* 主题,它应该会自动包含所需的主题和样式。 - Edric
2
使用上述任何主题都会使我的选项卡栏失去效果,并且未选择的选项卡文本不可见。不好。相反,将您的AppTheme从Theme.MaterialComponents.DayNight.DarkActionBar.Bridge扩展, 或者使用light而不是DayNight... - Sam
materialCalendarStyle 应该是 attr,而不是 item - IgorGanapolsky

0

有一个简单的解决方案!只需确保 theme.xml 是与您在 manifest 文件中使用的主题相同的主题。 在 Android 清单中: android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"> 在 theme.xml 中:


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接