【问题标题】:How can i apply a style to custom preference TimePicker?如何将样式应用于自定义首选项 TimePicker?
【发布时间】:2021-02-21 21:11:32
【问题描述】:

我想为我的 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>

我该如何解决这个问题?

【问题讨论】:

    标签: android xml material-design


    【解决方案1】:

    试试看:

    <?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>
    

    【讨论】:

    • 对不起,我忘了说我试过了,但它什么也没做,我也试过 android:theme 并且在预览中它可以工作,但是当我运行应用程序时它没有改变
    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多