更改TimePickerDialog样式

时间:2018-06-26 07:12:25

标签: android android-styles android-dialog android-timepicker

我想更改TimePicker的样式,但无法更改钟面和标题背景的颜色。这是我的XML样式代码-

    <style name="DateTimeDialog" parent="Theme.AppCompat.Dialog">
        <item name="android:colorBackground">@color/colorWhite</item>
        <item name="android:textColorPrimary">#000000</item>
        <item name="android:headerBackground">#000000</item>
        <item name="android:textColorSecondary">#000000</item>
        <item name="android:button">@color/colorWhite</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:colorAccent">@color/colorSecondary</item>
        <item name="android:textColor">@color/colorSecondaryDark</item>
    </style>

原色是海军蓝色,而强调色/次要颜色是浅蓝色(以防万一,您希望将其描绘出来)。这是当前的样子-

当前样式

enter image description here

我希望能够更改灰色背景。

注意:'android:background'属性在这里无效。它仅更改标题中显示的时间中“:”后面的微小区域的颜色。任何帮助/资源表示赞赏。谢谢!

2 个答案:

答案 0 :(得分:2)

试试看

    <style name="MyAppThemeFour" parent="Theme.AppCompat.Light">
    <item name="android:timePickerDialogTheme">@style/MyTimePickerDialogStyle</item>
</style>
<style name="MyTimePickerDialogStyle" parent="@style/ThemeOverlay.AppCompat.Dialog.Alert">
<item name="showTitle">false</item>
<item name="colorControlActivated">#ffd600</item>
<item name="colorAccent">#b71c1c</item>
<item name="android:textColorPrimary">#43a047</item>
<item name="android:textColorSecondary">#f44336</item>
</style> 

output

    <style name="MyTimePickerWidgetStyle" parent="@android:style/Widget.Material.TimePicker">
    <item name="android:headerBackground">#ffe082</item>
    <item name="android:numbersTextColor">#b71c1c</item>
    <item name="android:numbersInnerTextColor">#f44336</item>
    <item name="android:numbersSelectorColor">#33691e</item>
    <item name="android:numbersBackgroundColor">#ef9a9a</item>
    <item name="android:amPmTextColor">#9c27b0</item>
</style>

将其应用于时间选择器,例如:

<TimePicker
android:id="@+id/timePicker"
android:theme="@style/MyTimePickerWidgetStyle"
style="@style/MyTimePickerWidgetStyle"
. . . />

答案 1 :(得分:1)

您可以将 TimePickerDialog 构造函数与theme参数一起使用来指定主题。

TimePickerDialog(Context context, 
                 int theme, 
                 TimePickerDialog.OnTimeSetListener callBack, 
                 int hourOfDay, 
                 int minute, 
                 boolean is24HourView)

OR

将android:timePickerMode =“ spinner” 添加到XML

     <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner">
    </TimePicker>