问题
I have a ListView
containing a TextView
and a NumberPicker
for each row.
Whenever I start up the activity, the numbers of the NumberPicker
are white (the same color as the background).
I tried applying a style as suggested here, here, and here ; to no avail.
Any other suggestions?
-
NumberPicker.xml
<NumberPicker
android:id="@+id/npMaterialAmount"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="75dp"
style="@style/npColour"/>
-
Tried style 1:
<style name = "npColour" >
<item name = "android:textColor">#000000</item>
</style>
-
Tried style 2:
<style name = "npColour" parent ="@android:style/Theme.Holo.Light">
</style>
-
... and the list goes on...
回答1:
I know the question is old. I founded the answer here: Change the text color of NumberPicker
Copy:
This code should solve your problem. The problem you are experiencing is because during the construction of NumberPicker it captures the EditText textColor and assigns to to a paint so it can draw the numbers above and below the edit text with the same color.
import java.lang.reflect.Field;
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;
}
回答2:
Are you using a Formatter? I had to use setDisplayedValues to fix the problem. I assume you have already solved this problem, but I'm posting this anyway, I for one was desperate looking for an answer, solved it by accident.
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(5);
String[] periodicityValues = {"Semanal", "Mensal", "Bimestral", "Trimestral", "Semestral", "Anual"};
numberPicker.setDisplayedValues(periodicityValues);
来源:https://stackoverflow.com/questions/18120840/numberpicker-textcolour