更改TimePicker文字颜色

更改TimePicker文字颜色

本文介绍了更改TimePicker文字颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图改变我timepicker的文本颜色。但我找不到在哪里父风格所在。我都试过

I've been trying to change the textcolor of my timepicker. But I can't find where the parent style is located. I've tried both

<style name="MyTimePicker" parent="@android:style/Widget.Holo.TimePicker">
    <item name="android:textColor">@color/text</item>
</style>

<style name="MyTimePicker" parent="@android:style/Widget.TimePicker">
    <item name="android:textColor">@color/text</item>
</style>

我的的minSdkVersion 15。我的 targetSdkVersion 20。我已经rebuilded和清理我的项目。
我想我已经通过SO每一个类似的问题,没有人真的都为我提供一个解决方案。可能的工作唯一的答案就是使用某种库,但我不是这种解决方案的忠实粉丝。是的路径,从我使用什么不同父的东西,因为我是pretty的肯定,我应该能够以某种方式访问​​它?

My minSdkVersion is 15. My targetSdkVersion is 20. I have rebuilded and cleaned my project.
I think I've been through every similar question on SO and none of them really have provided a solution for me. The only answer that might work is using some sort of library, but I'm not a big fan of that solution. Is the path to the parent something different from what I'm using, because I'm pretty sure I should be able to access it somehow?

修改
这是怎样的主题应用;

Edit
This is how the theme is applied;

<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" />

在一张纸条,这是我收到的错误(忘记之前把它):

On a note this is the error I receive (forgot to place it before):

错误:错误检索父项:未发现的资源匹配给定名称@android:风格/ Widget.Holo.TimePicker

编辑2 一对夫妇的,我看到的问题,试图解决这个问题:

Edit 2A couple of the questions I've viewed to try to solve this:

  1. How我可以覆盖TimePicker改变文字颜色 - 我认为这个问题得到接近答案,但我不完全知道我需要做什么?我是否需要导入的android TimePicker风格融入我的项目?
  2. Set文本颜色为timepicker在定制应用主题 - 没有给出答复
  3. How我可以改变我的timepicker和日期选择器的文本颜色 - ?试过0票答案,但没有奏效
  4. How改变的DatePicker和TimePicker对话的默认颜色在机器人的 - ?,同样不能找到以类似的方式在TimePicker
  5. Android - 我该如何改变文字颜色的TimePicker - ?再次,无法找到实际TimePicker父
  1. How can I override TimePicker to change text color - I think this question gets as close to an answer, but I'm not entirely sure what I need to do? Do I need to import the android TimePicker style into my project?
  2. Set textcolor for timepicker in customized app theme - No answer is given.
  3. How can i change the textcolor of my timepicker and datepicker? - Tried the 0 votes answer, but it didn't work.
  4. How to change the default color of DatePicker and TimePicker dialog in Android? - Again can't find the TimePicker in a similar way.
  5. Android - How do I change the textColor in a TimePicker? - Again, can't find the actual TimePicker parent.

这些都可能是最好的提问/回答我的问题,但没有人帮助我。这将是很好得到这个确切的答案。

These are probably the best questions/answers to my problem, but none of them help me. It would be nice to get a definitive answer on this.

推荐答案

我已经联合保罗·伯克的答案和西蒙的回答以成功地编辑 TimePicker 的文本颜色。

I have combined Paul Burke's Answer and Simon's Answer to succesfully edit the text colour of the TimePicker.

下面是它是如何完成的:

Here's how it is accomplished:

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);
        }
    }
}

这篇关于更改TimePicker文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:33