web-dev-qa-db-fra.com

Erreur matérielleDatePicker: MaterialCalendarfulforthemeTentheme Attribut à définir dans votre thème de l'application

Ce que j'ai fait:

  1. Ajoutée implementation 'com.google.Android.material:material:1.1.0' dans des dépendances
  2. Régler Theme.MaterialComponents.Light.Bridge comme le parent au thème de l'application
     <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. Essayé de montrer le sélecteur de date en fragment.
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");

Quand j'ai dirigé le code, obtenez cette erreur

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).
6
bikram

Il suffit d'ajouter ce qui suit à votre thème de votre application.

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

Après avoir ajouté

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