更改TimePicker文本颜色

Bon*_*ono 9 xml android

我一直试图改变我的timepicker的textcolor.但我无法找到父样式所在的位置.我试过了两个

<style name="MyTimePicker" parent="@android:style/Widget.Holo.TimePicker">
    <item name="android:textColor">@color/text</item>
</style>
Run Code Online (Sandbox Code Playgroud)

<style name="MyTimePicker" parent="@android:style/Widget.TimePicker">
    <item name="android:textColor">@color/text</item>
</style>
Run Code Online (Sandbox Code Playgroud)

minSdkVersion是15.我targetSdkVersion20岁.我已经重建并清理了我的项目.
我想我已经完成了所有关于SO的类似问题,但他们都没有真正为我提供解决方案.唯一可行的答案是使用某种类型的库,但我不是那个解决方案的忠实粉丝.父母的路径是否与我正在使用的不同,因为我很确定我应该能够以某种方式访问​​它?

编辑
这是主题的应用方式;

<TimePicker
    style="@style/MyTimePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />
Run Code Online (Sandbox Code Playgroud)

注意这是我收到的错误(忘记放置它):

错误:检索项目的父项时出错:找不到与给定名称"@android:style/Widget.Holo.TimePicker"匹配的资源.

编辑2 我看过的几个问题试图解决这个问题:

  1. 如何覆盖TimePicker以更改文本颜色 - 我认为这个问题接近答案,但我不完全确定我需要做什么?我是否需要将android TimePicker样式导入到我的项目中?
  2. /sf/ask/1748151051/ - 没有给出答案.
  3. 如何更改timepicker和datepicker的textcolor? - 尝试了0票的答案,但没有奏效.
  4. 如何在Android中更改DatePicker和TimePicker对话框的默认颜色? - 再次无法以类似的方式找到TimePicker.
  5. Android - 如何更改TimePicker中的textColor? - 再次,找不到实际的TimePicker父级.

这些可能是我的问题的最佳问题/答案,但它们都没有帮助我.能够得到明确的答案真是太好了.

Bon*_*ono 23

我结合了Paul Burke的答案Simon的答案,成功地编辑了文本的颜色TimePicker.

以下是它的完成方式:

TimePicker time_picker; //Instantiated in onCreate()
Resources system;

private void set_timepicker_text_colour(){
    system = Resources.getSystem();
    int hour_numberpicker_id = system.getIdentifier("hour", "id", "android");
    int minute_numberpicker_id = system.getIdentifier("minute", "id", "android");
    int ampm_numberpicker_id = system.getIdentifier("amPm", "id", "android");

    NumberPicker hour_numberpicker = (NumberPicker) time_picker.findViewById(hour_numberpicker_id);
    NumberPicker minute_numberpicker = (NumberPicker) time_picker.findViewById(minute_numberpicker_id);
    NumberPicker ampm_numberpicker = (NumberPicker) time_picker.findViewById(ampm_numberpicker_id);

    set_numberpicker_text_colour(hour_numberpicker);
    set_numberpicker_text_colour(minute_numberpicker);
    set_numberpicker_text_colour(ampm_numberpicker);
}

private void set_numberpicker_text_colour(NumberPicker number_picker){
    final int count = number_picker.getChildCount();
    final int color = getResources().getColor(R.color.text);

    for(int i = 0; i < count; i++){
        View child = number_picker.getChildAt(i);

        try{
            Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
            wheelpaint_field.setAccessible(true);

            ((Paint)wheelpaint_field.get(number_picker)).setColor(color);
            ((EditText)child).setTextColor(color);
            number_picker.invalidate();
        }
        catch(NoSuchFieldException e){
            Log.w("setNumberPickerTextColor", e);
        }
        catch(IllegalAccessException e){
            Log.w("setNumberPickerTextColor", e);
        }
        catch(IllegalArgumentException e){
            Log.w("setNumberPickerTextColor", e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,这个答案现在可能已经过时了.我不久前遇到了一些可能有问题的东西(详情请参阅我的问题).否则你应该按照Vikram的回答.

  • 这对我有用.方便的小实用程序. (2认同)
  • @Bono这个代码在android Lollipop中不起作用.. hour_numberpicker ya其他所有都是由null Lollipop给出的.. (2认同)

Vik*_*ram 7

不知道为什么您需要为此深入研究 Java 反射 API。这是一个简单的造型问题。您需要覆盖的属性是:textColorPrimary

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    ....
    <item name="android:textColorPrimary">#ff0000</item>
</style>
Run Code Online (Sandbox Code Playgroud)

如果您使用TimePicker内部 a Dialog,则覆盖android:textColorPrimary对话框的主题。

就是这样。


Man*_*ddy 6

添加到Vikram答案,将主题设置为 timePicker 不要将其设置为样式

styles.xml

 <style name="timePickerOrange">
    <item name="android:textColorPrimary">@color/orange</item> <!-- color for digits -->
    <item name="android:textColor">@color/orange</item> <!-- color for colon -->
    <item name="android:colorControlNormal">@color/orange</item> <!-- color for (horizontal) delimeters -->
</style>
Run Code Online (Sandbox Code Playgroud)

对于 timePicker

<TimePicker
    android:theme="@style/timePickerOrange"
    android:id="@+id/timePicker_defaultTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="@font/hero_regular"
    android:timePickerMode="spinner"
    app:fontFamily="@font/hero_regular" />
Run Code Online (Sandbox Code Playgroud)