问题
My view has a white background, and it has to stay that way. I have a TimePicker on that white background.Everything is woking fine with android 2.3.3 but android 4.0.3 has a new timePicker style. The numbers have a very bright color. It is very hard to see them on the white background, and I didn't find a direct way to change the textColor. I don't want to change the background because it would look not so good.
Is there any way to override this and set the color of the numbers to black?
Sincerly, Wolfen
回答1:
Take a look at the android source for styles.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
You then set a style on your activity/timepicker it looks like you could do something like this:
<style name="MyTimePicker" parent="@android:style/Widget.TimePicker">
<item name="android:textColor">#000000</item>
</style>
or maybe (3.0 and above only)
<style name="MyHoloTimePicker" parent="@android:style/Widget.Holo.TimePicker">
<item name="android:textColor">#000000</item>
</style>
Then your xml would be:
<TimePicker
style="@style/MyHoloTimePicker"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
回答2:
use this function
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;
}
来源:https://stackoverflow.com/questions/10969513/how-can-i-override-timepicker-to-change-text-color