---------------重寫NumberPicker已達到修改顯示字體顏色大小------------------
public class TextColorNumberPicker extends NumberPicker { public TextColorNumberPicker(Context context) { super(context); } public TextColorNumberPicker(Context context, AttributeSet attrs) { super(context, attrs); } public TextColorNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void addView(View child) { super.addView(child); updateView(child); } @Override public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) { super.addView(child, index, params); updateView(child); } @Override public void addView(View child, android.view.ViewGroup.LayoutParams params) { super.addView(child, params); updateView(child); } public void updateView(View view) { if (view instanceof EditText) { //這裏修改顯示字體的屬性,主要修改顏色 // ((EditText) view).setTextColor(Color.parseColor("#BAA785")); ((EditText) view).setTextSize(12); } }
-----修改分割線顏色和線寬,需要在初始化控件後,通過反射進行動態設置顏色。---
**********注意:在NumberPicker 的 setDisplayedValues調用後調用如下方法:
public void setNumberPickerDividerColor(NumberPicker numberPicker) { NumberPicker picker = numberPicker; Field[] pickerFields = NumberPicker.class.getDeclaredFields(); for (Field pf : pickerFields) { if (pf.getName().equals("mSelectionDivider")) { pf.setAccessible(true); try { //設置分割線的顏色值 透明 pf.set(picker, new ColorDrawable(this.getResources().getColor(R.color.diver_gray))); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (Resources.NotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } // 分割線高度 for (Field pf2 : pickerFields) { if (pf2.getName().equals("mSelectionDividerHeight")) { pf2.setAccessible(true); try { int result = 3; pf2.set(picker, result); } catch (Exception e) { e.printStackTrace(); } break; } } }
*************************使用方法******************************
TextColorNumberPicker num_picker =(TextColorNumberPicker) dateTimeLayout.findViewById(R.id.num_picker); num_picker.setNumberPickerDividerColor(num_picker);