Nativescript Datepicker change text color

2019-08-14 06:06发布

问题:

I'm trying to change the datepicker color to white in Android. I've tried with colorPrimary in styles.xml with no luck:

<style name="AppTheme" parent="AppThemeBase">
    <item name="android:datePickerStyle">@style/SpinnerDatePicker</item>
</style>

<style name="SpinnerDatePicker" parent="android:Widget.Material.Light.DatePicker">
    <item name="colorPrimary">#FFF</item>
    <item name="android:datePickerMode">spinner</item>
</style>

Also, is there a way to use one color in a component and another in a second or am I bound to use one color for the whole app?

回答1:

You can't change the color of the DatePicker dynamically... at least it is not a trivial job. The thing is that even in native Android changing the color via code-behind can be achieved only with the Reflection API (see here for details).

To change the text color of the DatePicker there is one important thing to notice - you should apply android:textColorPrimary directly in the main app theme (see the comments here for details)

For example: values-v21/style.xml

<style name="AppTheme" parent="AppThemeBase">
    <item name="android:textColorPrimary">@color/ns_green</item> <!-- HERE -->
    <item name="android:datePickerStyle">@style/SpinnerDatePicker</item>
    <item name="android:timePickerStyle">@style/SpinnerTimePicker</item>
</style>

In the example above ns_green is defined in colors.xml

<color name="ns_green">#1eb234</color>