简体   繁体   English

如何更改 Android 应用程序中时间选择器的颜色?

[英]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) 这可以通过xml(仅适用于5.0+)轻松实现

v21/themes.xml 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! 编辑请注意,自Android支持库22.1.0起,有一个新的AppCompatDialog可用! http://android-developers.blogspot.de/2015/04/android-support-library-221.html 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第一个是 colorAccent,第二个是 colorControlActivated 最后一个不属于时间选择器,它属于对话框所以你应该应用类似于这篇文章的样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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