对于 DatePicker,我使用以下代码:
public static void setDatePickerTextColor(DatePicker dp, int color) {
LinearLayout l = (LinearLayout) dp.getChildAt(0);
if (l != null) {
l = (LinearLayout) l.getChildAt(0);
if (l != null) {
for (int i = 0; i < 3; i++) {
NumberPicker np = (NumberPicker) l.getChildAt(i);
if (np != null) {
setNumberPickerTextColor(np, color);
}
}
}
}
}
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("NumberPickerTextColor", e);
} catch (IllegalAccessException e) {
Log.w("NumberPickerTextColor", e);
} catch (IllegalArgumentException e) {
Log.w("NumberPickerTextColor", e);
}
}
}
return false;
}
虽然,它仅在 Lollipop 上进行了测试。