2017-05-20 61 views
1

我想更改DatePicker的文本顏色。更改日期選擇器文本顏色

我已經嘗試了幾個在這裏找到的建議,但沒有爲我工作。 文字顏色和選定的背景保持不變。

頂部標題背景確實發生了變化。

<style name="MyThemeDesign" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/btnColor</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item> 
</style> 

<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker"> 
     <item name="android:headerBackground">@color/orange</item> 
     <item name="android:text">@color/white</item> 
     <item name="android:calendarTextColor">@color/white</item> 
     <item name="android:dayOfWeekBackground">@color/white</item> 
     <item name="android:selectedWeekBackgroundColor">@color/colorRed</item> 
     <item name="android:yearListSelectorColor">@color/colorRed</item> 
     <item name="android:colorPrimary">@color/white</item> 
     <item name="android:colorPrimaryDark">@color/white</item> 
     <item name="android:colorBackgroundCacheHint">@color/colorRed</item> 
     <item name="colorAccent">@color/white</item> 
     <item name="android:textColorPrimary">@color/white</item> 
     <item name="android:datePickerMode">calendar</item> 
     <item name="android:minDate">01/01/2017</item> 
</style> 

回答

1

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    ... 
    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item> 
</style> 

<style name="MyDatePickerStyle" parent="@android:style/Widget.DeviceDefault.DatePicker"> 
    <item name="android:headerBackground">@color/red</item> 
    <item name="android:headerMonthTextAppearance">@style/MyDatePickerTextAppearance</item> 
</style> 

<style name="MyDatePickerTextAppearance" parent="TextAppearance.AppCompat.Body1"> 
    <item name="android:textColor">@drawable/my_color_state_list</item> 
</style> 

繪製/ my_color_state_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Month color --> 
    <item android:color="@color/yellow" android:state_selected="true"/> 
    <!-- Year color --> 
    <item android:color="@color/purple" /> 
</selector> 

如果你也想控制所選日期的顏色,那麼這個主題添加到style.xml

<style name="MyDatePickerTheme" parent="AppTheme"> 
    <item name="colorAccent">@color/blue</item> 
</style> 

在佈局:

<DatePicker 
    android:theme="@style/MyDatePickerTheme" 
    .../> 

結果:

enter image description here

相關問題