简体   繁体   English

如何在Android中更改时间选择器字体颜色?

[英]How to Change Time Picker Font Color in Android?

Hi I want to change the Font color of my Time Picker. 嗨,我想更改我的时间选择器的字体颜色。 I searched some other answers in Stackoverflow itself. 我在Stackoverflow本身中搜索了其他一些答案。 But I cant able to help myself on this. 但我无法为此提供帮助。

My targeted API is 19. Here is the list of things I tried. 我的目标API是19。这是我尝试过的事情的列表。

  1. I tried adding styles in Styles.xml in values-v11 and values-v14 folders. 我尝试在values-v11和values-v14文件夹的Styles.xml中添加样式。 Following is the code 以下是代码

<!-- language: lang-xml --> <style name="MyTimePicker" parent="@android:style/Widget.Holo.DatePicker"> <item name="android:textColor">@color/main_font</item> </style>
I tried adding it in values folder also. 我也尝试将其添加到values文件夹中。 But i Get the following error. 但是我得到以下错误。

@android:style/Widget.Holo.DatePicker requires API level 11 (current min is 8) @android:style / Widget.Holo.DatePicker需要API级别11(当前最小值为8)

I added styles to my TimePicker like 我在TimePicker中添加了样式,例如

<TimePicker
        android:id="@+id/alarm_timePicker"
        style="@style/MyTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

Nothing Changed. 没有改变。

2 . 2。 I tried adding some code from Stackoverflow within my activity. 我尝试在我的活动中从Stackoverflow添加一些代码。 Following is the code. 以下是代码。

public static boolean setNumberPickerTextColor(TimePicker numberPicker) {
    final int count = numberPicker.getChildCount();
    int color = R.color.main_font;
    for (int i = 0; i < count; i++) {
      View child = numberPicker.getChildAt(i);
      if (child instanceof EditText) {
        try {
          Field selectorWheelPaintField =
              numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");
          selectorWheelPaintField.setAccessible(true);
          ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
          ((EditText) child).setTextColor(color);
          numberPicker.invalidate();
          return true;
        } catch (NoSuchFieldException e) {
          //Log.w("setNumberPickerTextColor", e);
        } catch (IllegalAccessException e) {
          //Log.w("setNumberPickerTextColor", e);
        } catch (IllegalArgumentException e) {
          //Log.w("setNumberPickerTextColor", e);
        }
      }
    }
    return false;
    }

When I tried debugging it I got the error while setting the content view as 当我尝试调试它时,将内容视图设置为时出现错误

Attempt to invoke virtual method 'void android.widget.NumberPicker.setOnValueChangedListener(android.widget.NumberPicker$OnValueChangeListener)' on a null object reference 尝试在空对象引用上调用虚拟方法'void android.widget.NumberPicker.setOnValueChangedListener(android.widget.NumberPicker $ OnValueChangeListener)'

I made the same mistake. 我犯了同样的错误。 I suppose you found the answer yourself, but for others : Try to change your parent style from @android:style/Widget.Holo.DatePicker to @android:style/Widget.Holo.TimePicker . 我想您自己找到了答案,但是对于其他人:尝试将父样式从@android:style/Widget.Holo.DatePicker更改为@android:style/Widget.Holo.TimePicker

Same trick for @android:style/Widget.Material.Light.DatePicker , change it for @android:style/Widget.Material.Light.TimePicker @android:style/Widget.Material.Light.DatePicker相同技巧,将其更改为@android:style/Widget.Material.Light.TimePicker

The text colors are the textColorPrimary and textColorSecondary values of your theme. 文本颜色是主题的textColorPrimary和textColorSecondary值。 So you can create style like this one: 因此,您可以创建如下样式:

<style name="myTheme" parent="Theme.AppCompat.NoActionBar">

    <item name="android:textColorPrimary">#e90d8a</item>
    <item name="android:textColorSecondary">@color/white_primary_text</item>

   // The rest of your customization.

</style>

And on your activity use the setTheme method. 然后在您的活动中使用setTheme方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM