如何将样式应用于自定义首选项TimePicker?

时间:2020-11-09 22:42:41

标签: android xml material-design

我想为TimePicker首选项使用自定义样式来更改文本颜色,但我似乎无法覆盖默认主题。

这是我的主题资源文件:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Posture" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/purple_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="android:colorBackground">@color/purple_500</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorSecondary">@color/grey_dark</item>
</style>

我正在使用PreferenceDialogFragmentCompat和DialogPreference创建自定义TimePicker。

这是我在对话框中填充的布局:

<?xml version="1.0" encoding="utf-8"?>
<TimePicker
    style="@style/TimePickerStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/timePicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

我尝试创建一种样式,但是当我在时间选择器中使用它时,该样式不会被应用。

<style name="TimePickerStyle">
    <item name="android:textColor">@color/blue</item>
    <item name="android:textColorPrimary">@color/blue</item>
    <item name="android:textColorSecondary">@color/blue</item>
</style>

我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试一下:

<?xml version="1.0" encoding="utf-8"?>
<TimePicker
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/timePicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/TimePickerStyle"/>

和样式:

<style name="TimePickerStyle">
    <item name="android:numbersTextColor">@color/blue</item>
    <item name="android:numbersSelectorColor">@color/blue</item>
    <item name="android:numbersBackgroundColor">@color/blue</item>
</style>
相关问题