Android 中NumberPicker,DatePicker与DatePickerDialog中分割颜色的修改实例代码
程序员文章站
2023-12-16 14:21:52
android 中numberpicker,datepicker与datepickerdialog中分割颜色的修改实例代码
前言: 开发中,为了应用整体美观,需要保持控件...
android 中numberpicker,datepicker与datepickerdialog中分割颜色的修改实例代码
前言: 开发中,为了应用整体美观,需要保持控件的色调和主题色保持一致,
例如:numberpicker、datepicker和datepickerdialog的分割线颜色
一、numberpicker的分割线颜色
我把对numberpicker分割线颜色的方法写在一个工具类中,便于全局调用,代码如下:
public static void setnumberpickerdividercolor(context context, 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(context.getresources().getcolor(r.color.theme_pink))); } catch (illegalargumentexception e) { e.printstacktrace(); } catch (resources.notfoundexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } break; } } }
二、datepicker的分割线颜色
查看datapicker的源码可以看到,datepacker也是numberpicker实现的,所以主要思想,还是修改numberpicker分割线的颜色,代码如下:
public static void setdatepickerdividercolor(context context, 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(context.getresources().getcolor(r.color.theme_pink))); } catch (illegalargumentexception e) { e.printstacktrace(); } catch (resources.notfoundexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } break; } } } }
二、datepickerdialog的分割线颜色
实现思路:拿到dialog中datepicker就可以改变分割先的颜色,主要代码如下:
datepickerdialog datepickerdialog = new datepickerdialog(mcontext, 0, listener, year, month, day); datepickerdialog.settitle("选择年月"); datepickerdialog.show(); datepicker datepicker = datepickerdialog.getdatepickerstart(); //获取datepicker commonutil.setdatepickerdividercolor(mcontext, datepicker); //设置分割线颜色 /*修改按钮颜色这个必须在show或者create方法后面*/ button commitbtn = datepickerdialog.getbutton(dialoginterface.button_positive); //确认按钮 commitbtn.settextcolor(color.parsecolor("#e8615e"));
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!