2014-09-24 174 views
7

我一直在尝试更改我的timepicker的textcolor。但是我找不到父母的位置。我都试过更改TimePicker文本颜色

<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的类似问题,并没有一个真的为我提供了解决方案。可能工作的唯一答案是使用某种图书馆,但我不是那种解决方案的忠实粉丝。父母的路径与我使用的路径有什么不同,因为我很确定我应该能够以某种方式访问​​它?

编辑
这就是主题如何应用;

​​

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

Error:Error retrieving parent for item: No resource found that matches the given name '@android:style/Widget.Holo.TimePicker'.

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

  1. How can I override TimePicker to change text color - 我觉得这个问题接近答案,但我不完全确定我需要做什么?我是否需要将Android TimePicker样式导入到我的项目中?
  2. https://stackoverflow.com/questions/24973586/set-textcolor-for-timepicker-in-customized-app-theme - 未给出答案。
  3. How can i change the textcolor of my timepicker and datepicker? - 尝试了0票的答案,但它没有奏效。
  4. How to change the default color of DatePicker and TimePicker dialog in Android? - 再次无法以类似的方式找到TimePicker。
  5. Android - How do I change the textColor in a TimePicker? - 再次,找不到实际的TimePicker父级。

这些可能是我的问题的最佳问题/答案,但他们都没有帮助我。这将是很好的,得到一个明确的答案。

+0

第一种风格应该工作,你是如何应用这种风格的? – pskink 2014-09-24 11:51:50

+0

@pskink更新了答案 – Bono 2014-09-24 11:56:10

+0

试过[this](http://stackoverflow.com/a/21361885/3249477)? – Simas 2014-09-27 13:41:36

回答

20

我组合了Paul Burke's AnswerSimon's Answer以成功编辑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); 
     } 
    } 
} 
+2

这对我有效。方便的小实用程序。 – 2014-10-31 15:57:52

+1

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.widget.NumberPicker.getChildCount()'。final int count = number_picker.getChildCount (); – 2015-05-18 09:05:20

+0

@voidpointer如果你还有其他问题,请使用屏幕右上方的“问问题”按钮,你有什么是纯空指针例外。 – Bono 2015-05-18 09:21:44

3

A TimePicker实际上只是两个NumberPicker s。展望Widget.NumberPicker风格和布局,你会发现它使用

@style/TextAppearance.Large.Inverse.NumberPickerInputText 

不幸的是,TextAppearance.Large.Inverse.NumberPickerInputText不使用,你可以在你的主题设置的属性之一。所以,你有两个选择:

  1. 复制必要的类,使自己的NumberPickerTimePicker版本。 (你可以从图书馆中提取一些东西,如HoloEverywhere

  2. 使用黑客。

如果你想要去的第二条路线,你可以这样做:

private int mNumberPickerInputId = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Resources system = Resources.getSystem(); 

    // This is the internal id of the EditText used in NumberPicker (hack) 
    mNumberPickerInputId = 
     system.getIdentifier("numberpicker_input", "id", "android"); 

    // just used for full example, use your TimePicker 
    TimePicker timePicker = new TimePicker(this); 
    setContentView(timePicker); 

    final int hourSpinnerId = 
     system.getIdentifier("hour", "id", "android"); 
    View hourSpinner = timePicker.findViewById(hourSpinnerId); 
    if (hourSpinner != null) { 
     setNumberPickerTextColor(hourSpinner, Color.BLUE); 
    } 

    final int minSpinnerId = 
     system.getIdentifier("minute", "id", "android"); 
    View minSpinner = timePicker.findViewById(minSpinnerId); 
    if (minSpinner != null) { 
     setNumberPickerTextColor(minSpinner, Color.BLUE); 
    } 

    final int amPmSpinnerId = 
     system.getIdentifier("amPm", "id", "android"); 
    View amPmSpinner = timePicker.findViewById(amPmSpinnerId); 
    if (amPmSpinner != null) { 
     setNumberPickerTextColor(amPmSpinner, Color.BLUE); 
    } 
} 

private void setNumberPickerTextColor(View spinner, int color) { 
    TextView input = (TextView) spinner.findViewById(mNumberPickerInputId); 
    input.setTextColor(color); 
} 

编辑

经进一步调查,该黑客并没有真正很好地工作。它不会允许您更改NumberPicker上方/下方值的颜色。颜色在使用与之交互后也会重置。看来你唯一的选择是创建你自己的必要课程副本(上面的选项#1)。

+0

谢谢Paul“黑客”对我来说看起来很好。尽管我有几个问题;我如何获取上面和下面的数字以更改颜色?当我旋转/改变旋钮(选择一个不同的数字)时,它们会默认回到黑色。我如何防止这种情况? – Bono 2014-09-27 16:06:39

+0

@Bono请看我更新的答案。看起来像选项1可能是正确控制这一点的唯一方法。 :-( – 2014-09-27 22:34:41

4

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

<style name="AppTheme" parent="@android:style/Theme.Holo.Light"> 
    .... 
    <item name="android:textColorPrimary">#ff0000</item> 
</style> 

如果您使用的TimePicker一个Dialog内,覆盖在对话的主题android:textColorPrimary

就是这样。

+0

问题是它不能够' t找到'Theme.Holo.Light'父母,这就是文字颜色无法改变的原因。不幸的是,你的答案是关于某个东西有点不同。我会尽快编辑我的问题来描述这一点。 – Bono 2015-04-29 14:28:00

+0

@Bono不知道为什么它找不到'Theme.Holo.Light'。但父母在这里无关紧要。你应该保持父母的身份,最可能的是'Theme.AppCompat [.Light]'。 – Vikram 2015-04-29 22:04:22

+0

@Bono更多信息:提供'TimePicker'文本颜色的'attr'是'textColorPrimary'。所以,我们可以通过覆盖这个attr来改变文本的颜色。但是,这个attr也被用作所有'TextViews'的默认文本颜色。意思是,如果你不提供'android:textColor =“@ color/some_color”'_,它将使用'textColorPrimary'。最简单的方法是使用'ContextThemeWrapper'在java中创建'TimePicker'。看到这个问题已经很老了,我感谢你的评论。如果您需要使用'ContextThemeWrapper'的工作示例,请告诉我。 – Vikram 2015-04-29 23:41:27