[英]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).最终结果应该是这样的(抱歉质量差)。
With the Material Components for Android
you can use the new MaterialDatePicker
.借助Material Components for Android
的Material 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日期范围选择器
Additional reference额外参考
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.