更改日期选择器对话框颜色为Android 5.0 [英] Change Datepicker dialog color for Android 5.0
问题描述
是否有可能改变的日期选择器(以及timepicker)为Android 5.0的配色方案?
Is it possible to change the datepicker (and also the timepicker) colour scheme for android 5.0?
我tryed设置口音的颜色,但是这并不能正常工作(带或不带机器人:
):
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:
推荐答案
之所以尼尔的建议将导致在全屏的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
:
// 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
。因此,对于我们的更改生效,我们必须替换 datePickerStyle
内部的重写 datePickerDialogTheme
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。对话
:
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>
我们已经覆盖 datePickerStyle
的风格 MyDatePickerStyle
。家长的选择将再次取决于你的应用程序的基本主题是:要么 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>
目前,我们只覆盖 headerBackground
在缺省情况下设置为?ATTR / colorAccent
(这也是为什么尼尔建议在改变背景的作品)。但还有相当多的定制成为可能:
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的
的颜色。因此,仅仅覆盖 colorAccent
在 MyDatePickerDialogTheme
应该工作:
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
让你改变确定
&放大器的好处; 取消
文本颜色为好。不坏。
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!