自定义NumberPicker,更改字体颜色、分割线样式

来源:互联网 发布:怎样评价井柏然 知乎 编辑:程序博客网 时间:2024/05/22 04:39

---------------重写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);

0 0
原创粉丝点击