问题
I have implemented a custom Date Picker wherein I need to have custom colors for the number picker (Date picker uses Number picker inside) inside it as follows:

With the help of this answer I could make it look like this:

But as you see the actual date is getting shown in the yellow color instead of white. Moreover, I tried doing it at run time like this:
npMonth.setOnValueChangedListener(new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
EditText et = ((EditText) npMonth.getChildAt(0));
et.setTextColor(getResources().getColor(R.color.gold));
}
});
But still no luck.
How to change just the center text color to white and keep the top and bottom text yellow?
回答1:
The easiest way to do this
setNumberPickerTextColor(mNumberPicker);
public void setNumberPickerTextColor(NumberPicker numberPicker){
int color=Color.parseColor("#333333");
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();
}
catch(NoSuchFieldException e){
Log.e("setNumberPickerColor1", ""+e);
}
catch(IllegalAccessException e){
Log.e("setNumberPickerColor2", ""+e);
}
catch(IllegalArgumentException e){
Log.e("setNumberPickerColor3", ""+e);
}
}
}
}
来源:https://stackoverflow.com/questions/26887425/how-to-change-number-picker-center-text-color-in-android