Android NumberPicker和DatePicker分割线颜色设置
NumberPicker
/** * * 设置选择器的分割线颜色 * * @param numberPicker */ private void setDatePickerDividerColor(NumberPicker picker) { Field[] pickerFields = NumberPicker.class.getDeclaredFields(); for (Field pf : pickerFields) { if (pf.getName().equals("mSelectionDivider")) { pf.setAccessible(true); try { pf.set(picker, new ColorDrawable(Color.alpha(256)));//这里是将其隐藏,如有其它需要,修改这里的文字即可 } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } }
DatePicker
/** * * 设置时间选择器的分割线颜色 * @param datePicker */ private void setDatePickerDividerColor(DatePicker datePicker){ // 获取 mSpinners LinearLayout llFirst = (LinearLayout) datePicker.getChildAt(0); // 获取 NumberPicker LinearLayout mSpinners = (LinearLayout) llFirst.getChildAt(0); for (int i = 0; i < mSpinners.getChildCount(); i++) { NumberPicker picker = (NumberPicker) mSpinners.getChildAt(i); Field[] pickerFields = NumberPicker.class.getDeclaredFields(); for (Field pf : pickerFields) { if (pf.getName().equals("mSelectionDivider")) { pf.setAccessible(true); try { pf.set(picker, new ColorDrawable(ResourceUtil.getColor(R.color.com_text_red))); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } } }