简体   繁体   English

带有 AppCompat 的 Android Material Design Datepicker

[英]Android Material Design Datepicker with AppCompat

I'm trying to add the new Android 5.0 Material Design Datepicker to my pre 5.0 application using AppCompat.我正在尝试使用 AppCompat 将新的 Android 5.0 Material Design Datepicker 添加到我的 5.0 之前的应用程序中。 I've added我已经添加

compile "com.android.support:appcompat-v7:21.0.0"

to my build.gradle file and updated my Theme to:到我的 build.gradle 文件并将我的主题更新为:

<?xml version="1.0" encoding="utf-8"?>

<style name="AppTheme.Base" parent="@style/Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

but the Datepicker still looks like this:但 Datepicker 仍然是这样的:
旧日期选择器
And not like this:而不是这样:
材料设计日期选择器

Can anybody tell me how to get the new datepicker to work on pre 5.0 devices?有人能告诉我如何让新的日期选择器在 5.0 之前的设备上工作吗?

Thanks in advance.提前致谢。

Update:更新:

As well pointed out by jfcartier, there's now also MaterialDateTimePicker .正如 jfcartier 所指出的,现在还有MaterialDateTimePicker It's probably a nicer solution than the one below since it has a nice themable API.这可能是一个比下面更好的解决方案,因为它有一个很好的可主题化的 API。


You could try the android-betterpickers library.你可以试试android-betterpickers库。 It has a CalendarDatePickerDialog widget that looks like the one you want.它有一个CalendarDatePickerDialog小部件,看起来就像你想要的那样。 It provides a light and a dark theme, but for customizing colors you'd have to add it as a library project and change the code yourself.它提供了浅色和深色主题,但要自定义颜色,您必须将其添加为库项目并自己更改代码。

Usage is pretty straightforward once you add the library to your project.将库添加到项目后,用法非常简单。

    // Create date picker listener.
    CalendarDatePickerDialog.OnDateSetListener dateSetListener = new CalendarDatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(CalendarDatePickerDialog dialog, int year, int monthOfYear, int dayOfMonth) {
            // Set date from user input.
            Calendar date = Calendar.getInstance();
            date.set(Calendar.HOUR_OF_DAY, 9);
            date.set(Calendar.MINUTE, 0);
            date.set(Calendar.YEAR, year);
            date.set(Calendar.MONTH, monthOfYear);
            date.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            // Do as you please with the date.
        }
    };

    // Create dismiss listener.
    CalendarDatePickerDialog.OnDialogDismissListener dismissListener = new CalendarDatePickerDialog.OnDialogDismissListener() {
        @Override
        public void onDialogDismiss(DialogInterface dialoginterface) {
            // Do something when the user dismisses the dialog.
        }
    };

    // Show date picker dialog.
    CalendarDatePickerDialog dialog = new CalendarDatePickerDialog();
    dialog.setOnDateSetListener(dateSetListener);
    dialog.setOnDismissListener(dismissListener);
    dialog.setThemeDark(false);
    dialog.show(getSupportFragmentManager(), "DATE_PICKER_TAG");

The end result should look like this (sorry for the poor quality).最终结果应该是这样的(抱歉质量差)。

AOSP 日历选择器

Material components is the recommended way for date picker材料组件是日期选择器的推荐方式

With the Material Components for Android you can use the new MaterialDatePicker .借助Material Components for AndroidMaterial Components for Android您可以使用新的MaterialDatePicker

add the following to your build.gradle将以下内容添加到您的 build.gradle

implementation 'com.google.android.material:material:1.1.0'

For kotlin对于科特林

 val builder = MaterialDatePicker.Builder.datePicker()
 val picker = builder.build()
 picker.show(parentFragmentManager, "date_picker_tag")

For Java对于 Java

   MaterialDatePicker.Builder<Long> builder = 
   MaterialDatePicker.Builder.datePicker();
   MaterialDatePicker<Long> picker = builder.build();
   picker.show(getSupportFragmentManager(), picker.toString());

It supports the following configurations它支持以下配置

portrait - mode date picker纵向 - 模式日期选择器

在此处输入图片说明

landscape - mode date picker横向模式日期选择器

在此处输入图片说明

date range picker日期范围选择器

在此处输入图片说明

Mobile input picker移动输入选择器在此处输入图片说明

Additional reference额外参考

Here is the official design guideline是官方的设计指南

A complete demo app can be found here可以在此处找到完整的演示应用程序

I liked this library.我喜欢这个图书馆。 It's a clone of Flavien Laurent Date and Time Picker with some improvements.它是Flavien Laurent 日期和时间选择器的克隆,进行了一些改进。 Both of them are based on Official Google Date and Time Picker for Android 4.3+ but adapted for Android 2.1+.它们都基于Android 4.3+ 的官方 Google 日期和时间选择器,但适用于 Android 2.1+。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Material Design AppCompat - Android Material Design AppCompat Xamarin.Android 材料设计主题与 Appcompat - Xamarin.Android Material Design Theming with Appcompat 使用 Material Design 和 AppCompat 为 Android 中的按钮着色 - Coloring Buttons in Android with Material Design and AppCompat Android不支持Material Design设计和Appcompat-v7吗? - Material design and Appcompat-v7 not supported in Android? Android:使用AppCompat V7设置按钮的材质设计标高 - Android: Set material design elevation for button using AppCompat V7 Android Material Design 内联日期选择器问题 - Android Material Design Inline Datepicker issue 无法使用Appcompat在Android材质设计中为动作栏应用样式 - Unable apply styles for action bar in android material design with appcompat 材料设计,AppCompat和向后兼容性 - Material Design, AppCompat, and Backwards Compatibility Android:我可以使用Design Support Library而不是AppCompat for Material Design UI吗? - Android: Can I use Design Support Library instead of AppCompat for Material Design UI? 如何从材料设计日期选择器中获取月份 Android kotlin - How to get month from Material design Datepicker Android kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM