Android: How to change the color of the dateselector in Datepicker widget?

ⅰ亾dé卋堺 提交于 2019-11-29 09:26:56

问题


this is my first time posting a question.

I've successfully implemented a Datepicker in my app (targeting only SDK 21 and above). I also want to style my Datepicker to blend with the rest of the app. This is what I have:

<style name="MyDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
    <item name="android:colorAccent">@color/primDark</item>
</style>

<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
    <item name="android:headerBackground">@color/prim</item>
    <item name="android:calendarTextColor">@color/primDark</item>
    <item name="android:dayOfWeekBackground">@color/primDark</item>
    <item name="android:yearListSelectorColor">@color/accent</item>
    <item name="android:datePickerMode">calendar</item>
</style>

I can't find the attribute to change the color of the day selector in the calendar. No attribute listed here does that. However I was able to change the color of the year selector (see right side of the screenshot) with the attribute "yearListSelectorColor". How can I color the corresponding selector and selected text color on the left side?

To clarify, this is what my Datepicker looks like: Screenshots

So if someone knows how to get rid of that standard teal color, please tell me.

Thank you!


回答1:


Okay I think I got it.

I tried desperately to find an attribute in MyDatePickerStyle. The solution came to my mind when I rethought the whole inheritance hierarchy. I just had to add the colorAccent to the MyDatePickerDialogTheme.

<style name="MyDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <!-- this is new -->
    <item name="colorAccent">@color/accent</item>

    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
    <item name="android:colorAccent">@color/primDark</item>
</style>

<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
    <item name="android:headerBackground">@color/prim</item>
    <item name="android:calendarTextColor">@color/primDark</item>
    <item name="android:dayOfWeekBackground">@color/primDark</item>
    <item name="android:yearListSelectorColor">@color/accent</item>
    <item name="android:datePickerMode">calendar</item>
    <item name="android:minDate">01/01/2000</item>
</style>

Somehow I thought every theme gets the primary colors from the AppTheme automatically.

So hopefully this will help someone in the future.




回答2:


To change the color of the selector circle in DatePicker, use android:colorControlActivated attribute in your theme.



来源:https://stackoverflow.com/questions/30511743/android-how-to-change-the-color-of-the-dateselector-in-datepicker-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!