1. 程式人生 > >DatePicker設定時間選擇器的分割線顏色

DatePicker設定時間選擇器的分割線顏色

/**
 * 設定時間選擇器的分割線顏色
 *
 * @param datePicker
 */
private void setDatePickerDividerColor(DatePicker datePicker) {
    // Divider changing:

    // 獲取 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(getResources().getColor(R.color.transparent)));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}