简体   繁体   中英

How do I change the color of the timepicker in my Android app?

As you can see from the image below, I'm using the time picker in my app.
I need to change the color but don't know how...
Thanks for the help.

在此处输入图像描述

This can be easily achieved from xml (5.0+ only)

v21/themes.xml

<style name="Theme"
       parent="MyTheme.Base">

    <item name="android:dialogTheme">@style/Theme.Dialog</item> <!-- TimePicker dialog -->
    <item name="android:alertDialogTheme">@style/Theme.Dialog</item> <!-- Alert dialog -->

</style>

<style name="Theme.Dialog"
       parent="android:Theme.Material.Light.Dialog">

    <item name="android:colorAccent">@color/...</item>
    <item name="android:colorPrimary">@color/...</item>
    <item name="android:colorPrimaryDark">@color/...</item>

</style>

Edit Please note that as of Android Support Library 22.1.0 there is a new AppCompatDialog available! http://android-developers.blogspot.de/2015/04/android-support-library-221.html

Try this code

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TimePicker" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowIsFloating">true</item>
        <item name="colorAccent">@color/primary</item>
        <item name="colorControlActivated">@color/secondary</item>
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primary</item>
        <item name="android:buttonBarNegativeButtonStyle">@style/DatePickerTheme.ButtonBarButtonStyle</item>
        <item name="android:buttonBarPositiveButtonStyle">@style/DatePickerTheme.ButtonBarButtonStyle</item>
    </style>

    <style name="DatePickerTheme.ButtonBarButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:textColor">@color/primary</item>
    </style>
</resources>

The first one is colorAccent, the second is colorControlActivated and the last one does not belong to the timepicker, it belongs to dialog so you should apply the an style similar to this post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM