如何更改 Android 5.0 的 DatePicker 对话框颜色 [英] How to change DatePicker dialog color for Android 5.0
问题描述
是否可以更改 Android 5.0 的日期选择器(以及时间选择器)配色方案?
Is it possible to change the datepicker (and also the timepicker) color scheme for Android 5.0?
我尝试设置强调色,但这不起作用(无论有无 android
):
I've tried setting the accent colors, but this doesn't work (both with and without android
):
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/purple</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/purple_tint</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">@color/purple_tint</item>
来自原文:
这样的:
推荐答案
Neil的建议导致全屏DatePicker
的原因是父主题的选择:
The reason why Neil's suggestion results in a fullscreen DatePicker
is the choice of parent theme:
<!-- Theme.AppCompat.Light is not a dialog theme -->
<style name="DialogTheme" parent="**Theme.AppCompat.Light**">
<item name="colorAccent">@color/blue_500</item>
</style>
而且,如果你走这条路线,你必须在创建 DatePickerDialog
时指定主题:
Moreover, if you go this route, you have to specify the theme while creating the DatePickerDialog
:
// R.style.DialogTheme
new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//DO SOMETHING
}
}, 2015, 02, 26).show();
在我看来,这并不好.应该尝试将样式保持在 java 之外和 styles.xml/themes.xml 内部.
This, in my opinion, is not good. One should try to keep the styling out of java and inside styles.xml/themes.xml.
我同意 Neil 的建议,稍作改动(将父主题更改为 Theme.Material.Light.Dialog
)将为您提供所需的结果.但是,这是另一种方式:
I do agree that Neil's suggestion, with a bit of change (changing the parent theme to say, Theme.Material.Light.Dialog
) will get you the desired result. But, here's the other way:
在第一次检查时,我们遇到了 datePickerStyle
,它定义了以下内容:headerBackground
(您要更改的内容)、dayOfWeekBackground
、以及其他一些文本颜色和文本样式.
On first inspection, we come across datePickerStyle
which defines things such as: headerBackground
(what you are trying to change), dayOfWeekBackground
, and a few other text-colors and text-styles.
在您的应用主题中覆盖此属性将不起作用.DatePickerDialog
使用由属性 datePickerDialogTheme
分配的单独主题.因此,为了使我们的更改生效,我们必须在覆盖的 datePickerDialogTheme
内覆盖 datePickerStyle
.
Overriding this attribute in your app's theme will not work. DatePickerDialog
uses a separate theme assignable by the attribute datePickerDialogTheme
. So, for our changes to take affect, we must override datePickerStyle
inside an overriden datePickerDialogTheme
.
我们开始吧:
在应用的基本主题中覆盖 datePickerDialogTheme
:
Override datePickerDialogTheme
inside your app's base theme:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
....
<item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>
定义 MyDatePickerDialogTheme
.父主题的选择取决于您应用的基本主题:它可以是 Theme.Material.Dialog
或 Theme.Material.Light.Dialog
:
Define MyDatePickerDialogTheme
. The choice of parent theme will depend on what your app's base theme is: it could be either Theme.Material.Dialog
or Theme.Material.Light.Dialog
:
<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
</style>
我们已经用样式 MyDatePickerStyle
覆盖了 datePickerStyle
.父级的选择将再次取决于您应用的基本主题是什么:Widget.Material.DatePicker
或 Widget.Material.Light.DatePicker
.根据您的要求定义它:
We have overridden datePickerStyle
with the style MyDatePickerStyle
. The choice of parent will once again depend on what your app's base theme is: either Widget.Material.DatePicker
or Widget.Material.Light.DatePicker
. Define it as per your requirements:
<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">@color/chosen_header_bg_color</item>
</style>
目前,我们只覆盖默认设置为 ?attr/colorAccent
的 headerBackground
(这也是 Neil 建议在更改背景时起作用的原因).但是可以进行很多自定义:
Currently, we are only overriding headerBackground
which by default is set to ?attr/colorAccent
(this is also why Neil suggestion works in changing the background). But there's quite a lot of customization possible:
dayOfWeekBackground
dayOfWeekTextAppearance
headerMonthTextAppearance
headerDayOfMonthTextAppearance
headerYearTextAppearance
headerSelectedTextColor
yearListItemTextAppearance
yearListSelectorColor
calendarTextColor
calendarSelectedTextColor
如果您不想要这么多的控制(自定义),则无需覆盖 datePickerStyle
.colorAccent
控制大部分 DatePicker 的
颜色.因此,在 MyDatePickerDialogTheme
中仅覆盖 colorAccent
应该可以工作:
If you don't want this much control (customization), you don't need to override datePickerStyle
. colorAccent
controls most of the DatePicker's
colors. So, overriding just colorAccent
inside MyDatePickerDialogTheme
should work:
<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:colorAccent">@color/date_picker_accent</item>
<!-- No need to override 'datePickerStyle' -->
<!-- <item name="android:datePickerStyle">@style/MyDatePickerStyle</item> -->
</style>
重写 colorAccent
为您提供更改 OK
的额外好处 &CANCEL
文本颜色也是如此.还不错.
Overriding colorAccent
gives you the added benefit of changing OK
& CANCEL
text colors as well. Not bad.
这样您就不必向 DatePickerDialog 的
构造函数提供任何样式信息.一切都已正确接线:
This way you don't have to provide any styling information to DatePickerDialog's
constructor. Everything has been wired properly:
DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}, 2015, 5, 22);
dpd.show();
这篇关于如何更改 Android 5.0 的 DatePicker 对话框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!