更改Android 5.0的Datepicker对话框颜色 [英] Change Datepicker dialog color for Android 5.0
问题描述
是否可以更改android 5.0的datepicker(以及timepicker)配色方案?
我已经尝试设置重音色, t工作(无论是否有 android:
):
! - colorPrimary用于默认动作栏背景 - >
< item name =colorPrimary> @ color / purple< / item>
<! - colorPrimaryDark用于状态栏 - >
< item name =colorPrimaryDark> @ color / purple_tint< / item>
<! - colorAccent用作colorControlActivated
的默认值,用于绘制小部件 - >
< item name =colorAccent> @ color / purple_tint< / item>
从原始:
如下所示:
Neil的建议导致全屏 DatePicker
是父主题的选择:
;! - Theme.AppCompat.Light不是一个对话框主题 - >
< style name =DialogThemeparent =** Theme.AppCompat.Light **>
< item name =colorAccent> @ color / blue_500< / item>
< / style>
此外,如果你去这条路线,你必须在创建 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中。
我同意Neil的建议,有一点改变父主题可以说, Theme.Material.Light.Dialog
)将会得到你想要的结果。但是,另一种方式是:
在第一次检查时,我们遇到了 datePickerStyle
,它定义了以下内容: code> headerBackground (您想要更改的内容), dayOfWeekBackground
以及其他一些文本颜色和文本样式。 / p>
覆盖此应用程序主题中的此属性将无法正常工作。 DatePickerDialog
使用可由属性 datePickerDialogTheme
分配的单独主题。因此,为了使我们的更改生效,我们必须在覆盖 datePickerDialogTheme
之前覆盖 datePickerStyle
这里我们去:
覆盖 datePickerDialogTheme
在您的应用程式基础主题中:
< style name =AppBaseThemeparent =android:Theme.Material.Light> ;
....
< item name =android:datePickerDialogTheme> @ style / MyDatePickerDialogTheme< / item>
< / style>
定义 MyDatePickerDialogTheme
。父主题的选择将取决于您的应用程序的基础主题:它可以是 Theme.Material.Dialog
或 Theme.Material.Light。对话框
:
< style name =MyDatePickerDialogThemeparent =android:Theme.Material。 Light.Dialog>
< item name =android:datePickerStyle> @ style / MyDatePickerStyle< / item>
< / style>
我们已经覆盖了 datePickerStyle
code> MyDatePickerStyle 。父母的选择将再次取决于您的应用程序的基础主题: Widget.Material.DatePicker
或 Widget.Material.Light.DatePicker
。根据您的要求定义:
< style name =MyDatePickerStyleparent =@ android:style / Widget.Material .Light.DatePicker>
< item name =android:headerBackground> @ color / chosen_header_bg_color< / item>
< / style>
目前,我们只是覆盖 headerBackground
默认情况下设置为?attr / colorAccent
(这也是Neil建议改变背景的原因)。但是有很多定制可能:
dayOfWeekBackground
dayOfWeekTextAppearance
headerMonthTextAppearance
headerDayOfMonthTextAppearance
headerYearTextAppearance
headerSelectedTextColor
yearListItemTextAppearance
yearListSelectorColor
calendarTextColor
calendarSelectedTextColor
如果您不想要这么多的控件(自定义),则不需要覆盖 datePickerStyle
。 colorAccent
控制大部分 DatePicker的
颜色。所以,在 MyDatePickerDialogTheme
内的 c> colorAccent 中,只能执行
< style name =MyDatePickerDialogThemeparent =android:Theme.Material.Light.Dialog>
< item name =android:colorAccent> @ color / date_picker_accent< / item>
<! - 不需要重写'datePickerStyle' - >
<! - < item name =android:datePickerStyle> @ style / MyDatePickerStyle< / item> - >
< / style>
覆盖 colorAccent
为您提供额外的好处更改确定
& CANCEL
文字颜色也是如此。不是很糟糕。
这样你就不必向 DatePickerDialog的
构造函数提供任何样式信息。一切都已经正确连接:
DatePickerDialog dpd = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener(){
@覆盖
public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth){
}
},2015,5,22);
dpd.show();
Is it possible to change the datepicker (and also the timepicker) colour scheme for android 5.0?
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>
From original:
To something like this:
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>
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();
This, in my opinion, is not good. One should try to keep the styling out of java and inside styles.xml/themes.xml.
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:
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.
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
.
Here we go:
Override datePickerDialogTheme
inside your app's base theme:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
....
<item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>
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>
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>
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
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>
Overriding colorAccent
gives you the added benefit of changing OK
& CANCEL
text colors as well. Not bad.
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屋!