我不知道您的日期选择器主题的父级主题是什么。假设您拥有自定义的日期选择器主题,如下所示:
<style name="yourCustomStyle" parent="Theme.AppCompat.Light">
Theme.AppCompat.Light
,这将带您进入一个新的方式,您可以在其中找到您需要的内容。这里只涉及标题文本的问题,但您可能希望更改其他视图的颜色,所以这是您需要查看的地方。android:textColorPrimaryInverse
<style name="yourCustomStyle" parent="Theme.AppCompat.Light">
<item name="colorAccent">@color/blue</item>
<item name="android:textColorPrimaryInverse">@color/yellow</item>
.. other
</style>
可以自由使用您自己的颜色和代码(我从这里复制了代码),它会完成工作!
new DatePickerDialog(MainActivity.this, R.style.yourCustomStyle, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}, 2015, 02, 26).show();
图片:android:textColorPrimaryInverse与Theme.AppCompat.Dialog
我可以通过为我的日期选择器DialogFragment提供一个自定义主题CustomDatePickerDialogTheme
来更改标题中日期的颜色:
<style name="CustomDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:datePickerStyle">@style/CustomDatePickerStyle</item>
</style>
<style name="CustomDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
<item name="android:headerMonthTextAppearance">@style/HeaderTextStyle</item>
</style>
<style name="HeaderTextStyle" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@color/colorAccent</item>
</style>
HeaderTextStyle
生效了(将 TextAppearance.Medium
更改为 TextAppearance.Large
可以更改字体大小),但颜色被忽略了。 - TimSim您可以按照以下步骤更改DateTimePicker的标题和正文背景:
进入android/app/src/main/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">#000000</item>
**Add these two files !!!**
<item name="android:timePickerDialogTheme">@style/ClockTimePickerDialog</item>
<item name="android:datePickerDialogTheme">@style/DialogDatePicker.Theme</item>
</style>
**Add these files also**
<style name="DialogDatePicker.Theme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="colorAccent">#000000</item>
</style>
<style name="ClockTimePickerDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColorPrimary">#000</item>
</style>
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>
你可以试试这个,它可能会对你有所帮助:
将你的styles.xml
编辑为:
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/white</item>
</style>
在你的代码中添加以下几行:
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();
colorAccent
不会改变标题文本颜色,只会改变 OK 和 CANCEL 按钮以及日历中当前选定的日期(现在是青绿色)。 - TimSim