如何更改Android 5.0的DatePicker对话框颜色 [英] How to change DatePicker dialog color for Android 5.0
问题描述
是否可以更改android 5.0的datepicker(以及timepicker)配色方案?
Is it possible to change the datepicker (and also the timepicker) colour scheme for android 5.0?
我尝试设置强调色,但这不起作用(带和不带android:
的情况):
I've tryed setting the accent colours, 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.
我确实同意尼尔的建议,并做了一些改动(将父主题更改为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's
颜色.因此,仅覆盖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's
构造函数提供任何样式信息.一切都已正确接线:
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屋!