带有 AppCompat 的 Android Material Design Datepicker [英] Android Material Design Datepicker with AppCompat

查看:17
本文介绍了带有 AppCompat 的 Android Material Design Datepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AppCompat 将新的 Android 5.0 Material Design Datepicker 添加到我的 5.0 之前的应用程序中.我已经添加

I'm trying to add the new Android 5.0 Material Design Datepicker to my pre 5.0 application using AppCompat. I've added

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

到我的 build.gradle 文件并将我的主题更新为:

to my build.gradle file and updated my Theme to:

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

但 Datepicker 仍然看起来像这样:

而不是这样:

but the Datepicker still looks like this:

And not like this:

谁能告诉我如何让新的日期选择器在 5.0 之前的设备上工作?

Can anybody tell me how to get the new datepicker to work on pre 5.0 devices?

提前致谢.

推荐答案

更新:

正如 jfcartier 所指出的,现在还有 MaterialDateTimePicker.这可能是一个比下面更好的解决方案,因为它有一个很好的主题化 API.

As well pointed out by jfcartier, there's now also MaterialDateTimePicker. It's probably a nicer solution than the one below since it has a nice themable API.

您可以尝试 android-betterpickers 库.它有一个 CalendarDatePickerDialog 小部件,看起来就像你想要的那样.它提供了浅色和深色主题,但要自定义颜色,您必须将其添加为库项目并自行更改代码.

You could try the android-betterpickers library. It has a CalendarDatePickerDialog widget that looks like the one you want. 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).

这篇关于带有 AppCompat 的 Android Material Design Datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆