一直好好的,突然某个版本,TimePicker的字看不见了。
布局长这样
<TimePicker
android:id="@+id/new_act_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
然后百度到如下代码:
private void set_timepicker_text_colour(TimePicker time_picker){
Resources system = Resources.getSystem();
int hour_numberpicker_id = system.getIdentifier("hour", "id", "android");
int minute_numberpicker_id = system.getIdentifier("minute", "id", "android");
int ampm_numberpicker_id = system.getIdentifier("amPm", "id", "android");
NumberPicker hour_numberpicker = (NumberPicker) time_picker.findViewById(hour_numberpicker_id);
NumberPicker minute_numberpicker = (NumberPicker) time_picker.findViewById(minute_numberpicker_id);
NumberPicker ampm_numberpicker = (NumberPicker) time_picker.findViewById(ampm_numberpicker_id);
set_numberpicker_text_colour(hour_numberpicker);
set_numberpicker_text_colour(minute_numberpicker);
set_numberpicker_text_colour(ampm_numberpicker);
}
private void set_numberpicker_text_colour(NumberPicker number_picker){
final int count = number_picker.getChildCount();
//这里就是要设置的颜色,修改一下作为参数传入会更好
final int color = getResources().getColor(R.color.color_5_5_5);
for(int i = 0; i < count; i++){
View child = number_picker.getChildAt(i);
try{
Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint)wheelpaint_field.get(number_picker)).setColor(color);
((EditText)child).setTextColor(color);
number_picker.invalidate();
}
catch(NoSuchFieldException e){
Log.w("setColor", e.toString());
}
catch(IllegalAccessException e){
Log.w("setColor", e.toString());
}
catch(IllegalArgumentException e){
Log.w("setColor", e.toString());
}
}
}
加入以后,奔溃,不知原因。
研究源码:
public TimePicker(Context context) {怎么还有两个mode?我内心是奔溃的。
this(context, null);
}
public TimePicker(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.timePickerStyle);
}
public TimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public TimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.TimePicker, defStyleAttr, defStyleRes);
final int mode = a.getInt(R.styleable.TimePicker_timePickerMode, MODE_SPINNER);
a.recycle();
switch (mode) {
case MODE_CLOCK:
mDelegate = new TimePickerClockDelegate(
this, context, attrs, defStyleAttr, defStyleRes);
break;
case MODE_SPINNER:
default:
mDelegate = new TimePickerSpinnerDelegate(
this, context, attrs, defStyleAttr, defStyleRes);
break;
}
}
MODE_CLOCK 模式,是模拟手表盘,后加的模式。
MODE_SPINNER 经典模式,也是最早的模式。
突然解惑,为什么我的TimePicker在不同版本手机上长的不一样了。
简单修改一下布局:
<TimePicker
android:id="@+id/new_act_time_picker"
android:timePickerMode="spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
为了照顾老版本,就用这种模式吧。
再套用上面的代码,字体颜色改掉。