使用AppCompat的Android材质设计Datepicker [英] Android Material Design Datepicker with AppCompat
问题描述
我正在尝试使用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:
有人可以告诉我如何让新的datepicker在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材质设计Datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!