如何更改 Android 5.0的 DatePicker 对话框颜色

是否可以更改 Android 5.0的数据采集器(以及计时器)配色方案?

我试过设置重音的颜色,但是这个不起作用(无论有没有 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>

原著:

Original

像这样的东西:

Recolored

121796 次浏览

试试这个。

密码

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();

Styles.xml 文件中的 Style

编辑-改变主题。应用程序。光。建议的对话框

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/blue_500</item>
</style>

尼尔的建议之所以产生全屏 DatePicker,是因为选择了父母主题:

<!-- 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 中使用样式。

我确实同意尼尔的建议,只要稍加改动(把父主题改为 Theme.Material.Light.Dialog) ,就能得到你想要的结果。但是,还有另一种方法:

在第一次检查时,我们遇到了 datePickerStyle,它定义了诸如: headerBackground(您试图改变的内容)、 dayOfWeekBackground以及其他一些文本颜色和文本样式。

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 在重写的 ABC1中重写 datePickerStyle.

开始了:

在应用程序的基本主题中覆盖 datePickerDialogTheme:

<style name="AppBaseTheme" parent="android:Theme.Material.Light">
....
<item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>

定义 MyDatePickerDialogTheme。父主题的选择取决于应用程序的基本主题是什么: 它可以是 Theme.Material.DialogTheme.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

如果不需要这么多的控制(自定义) ,则不需要重写 datePickerStylecolorAccent控制 DatePicker's的大部分颜色。因此,在 MyDatePickerDialogTheme中覆盖 colorAccent应该可以工作:

<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给你额外的好处改变 OKCANCEL文本颜色以及。不错。

这样你就不需要向 DatePickerDialog's构造函数提供任何样式信息了,所有的东西都被正确地连接起来了:

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.R.style.Theme_DeviceDefault_Light_Dialog这样的默认主题。

new DatePickerDialog(MainActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//DO SOMETHING
}
}, 2015, 02, 26).show();

不需要创建主题,只需将其写入对话框创建对象中即可

DatePickerDialog datePicker = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT,this, mYear, mMonth, mDay);

按照这个它会给你所有的类型日期选择器样式 真的是工作

Http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/

试试这个,为我工作

把两个选项,colorAccentandroid:colorAccent

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
....
<item name="android:dialogTheme">@style/AppTheme.DialogTheme</item>
<item name="android:datePickerDialogTheme">@style/Dialog.Theme</item>
</style>


<style name="AppTheme.DialogTheme" parent="Theme.AppCompat.Light.Dialog">


<!-- Put the two options, colorAccent and android:colorAccent. -->
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorPrimary</item>
</style>

我有一个问题,时间选择器按钮没有在屏幕上看到的 API 24在机器人。(空气污染指数21 +) 它是由

<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:colorAccent">@color/colorPrimary2</item></style>


<style name="Theme" parent="BBaseTheme">
<item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>
<style name="AppThemeDatePicker" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">@color/select2</item>
<item name="android:colorAccent">@color/select2</item>
<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
</style>




<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">@color/select2</item>
</style>

用于更改年份列表项的文本颜色(特定于 ANDROID 5.0)

只需在日期选择器对话框样式中设置某些文本颜色。由于某些原因,设置 yearListItemTextAppearance标志并不反映年度列表的任何变化。

<item name="android:textColor">@android:color/black</item>

创建一个新样式

<!-- Theme.AppCompat.Light.Dialog -->
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/blue_500</item>
</style>

Java 代码:

父主题是这里的关键。 选择你的颜色口音

DatePickerDialog = new DatePickerDialog(context,R.style.DialogTheme,this,now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH);

结果:

enter image description here

Kotlin 2021年

// set date as button text if pressed
btnDate.setOnClickListener(View.OnClickListener {


val dpd = DatePickerDialog(
this,
{ view, year, monthOfYear, dayOfMonth ->
val selectDate = Calendar.getInstance()
selectDate.set(Calendar.YEAR, year)
selectDate.set(Calendar.MONTH, monthOfYear)
selectDate.set(Calendar.DAY_OF_MONTH, dayOfMonth)


var formatDate = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val date = formatDate.format(selectDate.time)
Toast.makeText(this, date, Toast.LENGTH_SHORT).show()
btnDate.text = date
}, 1990, 6, 6
)


val calendar = Calendar.getInstance()
val year = calendar[Calendar.YEAR]
val month = calendar[Calendar.MONTH]
val day = calendar[Calendar.DAY_OF_MONTH]
dpd.datePicker.minDate = GregorianCalendar(year - 90, month, day, 0, 0).timeInMillis
dpd.datePicker.maxDate = GregorianCalendar(year - 10, month, day, 0, 0).timeInMillis
dpd.show()
})

Styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- This is main Theme Style for your application! -->
<item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>


<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Dialog">
<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
</style>


<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.DatePicker">
<item name="android:headerBackground">#A500FF</item>
</style>

enter image description here