我已经实现了一个自定义日期选择器,其中我需要为其中的数字选择器(日期选择器使用数字选择器)提供自定义颜色,如下所示:
在this answer的帮助下,我可以看起来像这样:
但正如您所看到的那样,实际日期会以黄色而不是白色显示.
而且,我尝试在运行时这样做:
npMonth.setOnValueChangedListener(new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker,int oldVal,int newVal) { EditText et = ((EditText) npMonth.getChildAt(0)); et.setTextColor(getResources().getColor(R.color.gold)); } });
但仍然没有运气.
如何将中心文本颜色更改为白色并将顶部和底部文本保持为黄色?
解决方法
最简单的方法
setNumberPickerTextColor(mNumberPicker); public void setNumberPickerTextColor(NumberPicker numberPicker){ int color=Color.parseColor("#333333"); final int count = numberPicker.getChildCount(); for(int i = 0; i < count; i++){ View child = numberPicker.getChildAt(i); if(child instanceof EditText){ try{ Field selectorWheelPaintField = numberPicker.getClass().getDeclaredField("mSelectorWheelPaint"); selectorWheelPaintField.setAccessible(true); ((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color); ((EditText)child).setTextColor(color); numberPicker.invalidate(); } catch(NoSuchFieldException e){ Log.e("setNumberPickerColor1",""+e); } catch(IllegalAccessException e){ Log.e("setNumberPickerColor2",""+e); } catch(IllegalArgumentException e){ Log.e("setNumberPickerColor3",""+e); } } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。