不同设备上的“时间选择器”对话框主题

时间:2018-08-01 07:35:32

标签: android android-styles android-timepicker

在不同设备上设置TimePickerDialog的样式时遇到问题

我已经创建了样式

<style name="MyTimePickerStyle" parent="Theme.AppCompat.Light.Dialog">

    <item name="android:colorAccent">@android:color/transparent</item>

    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:textColorSecondary">@color/white50</item>
    <item name="android:headerTimeTextAppearance">@color/white50</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:windowBackground">@drawable/gradient_dialog</item>
    <item name="android:headerBackground">@drawable/gradient_dialog</item>
    <item name="android:textColorHighlight">@color/white</item>
    <item name="android:colorControlActivated">@color/white50</item>
    <item name="android:numbersBackgroundColor">@color/dark_three</item>
    <item name="android:numbersInnerTextColor" tools:ignore="NewApi">@color/white50</item>
    <item name="android:amPmTextColor">@color/white50</item>
    <item name="android:numbersTextColor">@color/white50</item>
    <item name="android:numbersSelectorColor">@color/lilac</item>

    <item name="buttonBarNegativeButtonStyle">@style/DialogButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/DialogButtonStyle</item>
    <item name="buttonBarNeutralButtonStyle">@style/DialogButtonStyle</item>
    <item name="android:buttonBarNeutralButtonStyle">@style/DialogButtonStyle</item>
    <item name="android:buttonBarPositiveButtonStyle">@style/DialogButtonStyle</item>
    <item name="android:buttonBarNegativeButtonStyle">@style/DialogButtonStyle</item>

</style>

在不同的设备和api lvl-s上看起来像这样 Android 26 enter image description here Android 23 enter image description here Android 21 enter image description here

我都尝试像

一样创建它
TimePickerDialog(this,R.style.MyTimePickerStyle, listener, hour, minute, true)

或将其添加为活动样式的样式

<item name="android:timePickerDialogTheme">@style/MyTimePickerStyle</item>

两个案例都给出了相同的结果

使用自定义视图创建对话框并为TimePIcker添加样式,因为从@android:style/Widget.Material.Light.TimePicker继承的视图在不同的设备上会产生不同的结果,但这仍然不是应该的。

任何想法如何正确设置TimePicker的样式?

欢呼 Wojtek

2 个答案:

答案 0 :(得分:0)

我确实为DatePicker创建了一个主题,并且通过使用如下属性进行了工作:

<style name="DatePickerTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlActivated">@color/colorAccent</item>
</style>

尝试通过添加android:前缀

答案 1 :(得分:0)

在可绘制对象中添加timepicker_drawable.xml

 <?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetLeft="16dp"
       android:insetTop="16dp"
       android:insetRight="16dp"
       android:insetBottom="16dp">

    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="#262d37" />
    </shape>

</inset>

添加这是styles.xml

    <style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorControlNormal">@color/darkBackground</item>
    <item name="colorControlActivated">@color/textColor</item>
    <item name="textColorAlertDialogListItem">@color/whiteTextColor</item>
    <item name="colorAccent">@color/textColor</item>
    <item name="android:textColorPrimary">@color/whiteTextColor</item>
    <item name="android:windowBackground">@drawable/timepicker_drawable</item>
</style>

根据您的要求更改颜色,然后TimePickerDialog timePickerDialog = new TimePickerDialog( context, R.style.MyDialogTheme, listener, startHour, startMinute, is24HourFormat);