我有一个Android应用程序,我想更改日期选择器的分隔颜色。
这适用于SDK 20。
SDK 21 =>显示默认的蓝色分隔线 SDK 23 =>显示灰色分隔符
SDK 15 - 20 =>显示我的绿色分隔符(正确)
这是我的代码:
// DatePicker Set Divider Color
private void preparePicker() {
try {
Field datePickerFields[] = DP.getClass().getDeclaredFields();
for (Field field : datePickerFields) {
if ("mSpinners".equals(field.getName())) {
field.setAccessible(true);
Object spinnersObj = field.get(DP);
LinearLayout mSpinners = (LinearLayout) spinnersObj;
NumberPicker monthPicker = (NumberPicker) mSpinners.getChildAt(0);
NumberPicker dayPicker = (NumberPicker) mSpinners.getChildAt(1);
NumberPicker yearPicker = (NumberPicker) mSpinners.getChildAt(2);
setDividerColor(monthPicker);
setDividerColor(dayPicker);
setDividerColor(yearPicker);
break;
}
}
} catch (Exception ex) {
Log.e("-->", "Unable to change date dialog");
}
}
private void setDividerColor(NumberPicker picker) {
Field[] numberPickerFields = NumberPicker.class.getDeclaredFields();
for (Field field : numberPickerFields) {
if (field.getName().equals("mSelectionDivider")) {
field.setAccessible(true);
try {
field.set(picker, getResources().getDrawable(R.drawable.picker_divider));
} catch (IllegalArgumentException e) {
Log.e("-->", "Illegal Argument Exception");
e.printStackTrace();
} catch (Resources.NotFoundException e) {
Log.e("-->", "Resources NotFound");
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.e("-->", "Illegal Access Exception");
e.printStackTrace();
}