如何覆盖时间选择器以更改文本颜色



我的视图有白色背景,它必须保持这种状态。我在白色背景上有一个时间选择器。Android 2.3.3 一切都很好,但 android 4.0.3 有了新的 timePicker 风格。这些数字具有非常明亮的颜色。在白色背景上很难看到它们,而且我没有找到直接的方法来更改文本颜色。我不想改变背景,因为它看起来不太好。

有没有办法覆盖它并将数字的颜色设置为黑色?

真诚地,沃尔芬

看看 android 的样式来源.xml

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml

然后,您在活动/时间选择器上设置样式,看起来您可以执行以下操作:

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

或者也许(仅限 3.0 及以上)

<style name="MyHoloTimePicker" parent="@android:style/Widget.Holo.TimePicker">
    <item name="android:textColor">#000000</item>
</style>

那么你的 xml 将是:

<TimePicker
 style="@style/MyHoloTimePicker"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

使用此函数

public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
final int count = numberPicker.getChildCount();
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;

}

最新更新