版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_33323480/article/details/84136858
一、首先了解DatePicker源码的布局
年、月、日 是由3个numberPicker组成 通过发射获取到NumberPicker 如果不想显示天数 ,重新布局
lps.width = (ScreenUtils.getScreenWidth(context)- DensityUtil.dip2px(context,20))/count;
按权重划分在调用的activity里把日隐藏
((ViewGroup)((ViewGroup) datepicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
二、对DatePicker的基础设置
/** * 设置时间的最大值 */ Date date = new Date(); datepicker.setMaxDate(date.getTime()); datepicker.setDate(start_date); /** * 设置最小日期 */ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long min_long = 0; try { min_long=sdf.parse("2018-07-01 00:00:00").getTime(); } catch (ParseException e) { e.printStackTrace(); } datepicker.setDividerColor(0xffdddddd); datepicker.setPickerMargin(0,3); datepicker.setMinDate(min_long);
三 自定义View 定义一个CustomDatePicker xml中引用
public class CustomDatePicker extends DatePicker { private List<NumberPicker> mPickers; private Context context; public CustomDatePicker(Context context) { super(context); this.context = context; findNumberPicker(); } public CustomDatePicker(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; findNumberPicker(); } public CustomDatePicker(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; findNumberPicker(); } /** * 得到控件里面的numberpicker组件 */ private void findNumberPicker() { mPickers = new ArrayList<NumberPicker>(); LinearLayout llFirst = (LinearLayout) getChildAt(0); LinearLayout mSpinners = (LinearLayout) llFirst.getChildAt(0); for (int i = 0; i < mSpinners.getChildCount(); i++) { NumberPicker picker = (NumberPicker) mSpinners.getChildAt(i); mPickers.add(i, picker); } } /** * 设置时间 * @param strDate yyyy-mm-dd */ public void setDate(String strDate) { int day, month, year; if (!TextUtils.isEmpty(strDate)) { String[] dateValues = strDate.split("-"); if (dateValues.length == 3) { year = Integer.parseInt(dateValues[0]); month = Integer.parseInt(dateValues[1]) - 1; day = Integer.parseInt(dateValues[2]); updateDate(year, month, day); return; } } //error Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); day = calendar.get(Calendar.DAY_OF_MONTH); month = calendar.get(Calendar.MONTH); year = calendar.get(Calendar.YEAR); updateDate(year, month, day); } /** * 获得时间 * @return yyyy-mm-dd */ public String getDate() { StringBuilder sbDate = new StringBuilder(); sbDate.append(format2Digits(getYear())).append("-") .append(format2Digits(getMonth()+1)).append("-") .append(format2Digits(getDayOfMonth())); return sbDate.toString(); } private String format2Digits(int value) { return String.format("%02d",value); } /** * 设置picker间隔 * * @param margin */ public void setPickerMargin(int margin,int count) { for (NumberPicker picker : mPickers) { LinearLayout.LayoutParams lps = (LinearLayout.LayoutParams) picker.getLayoutParams(); lps.setMargins(margin, 0, margin, 0); lps.width = (ScreenUtils.getScreenWidth(context)- DensityUtil.dip2px(context,20))/count; lps.height = DensityUtil.dip2px(context,180); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { lps.setMarginStart(margin); lps.setMarginEnd(margin); } picker.setLayoutParams(lps); } } /** * 设置时间选择器的分割线颜色 */ public void setDividerColor(int color) { for (int i = 0; i < mPickers.size(); i++) { NumberPicker picker = mPickers.get(i); try { Field pf = NumberPicker.class.getDeclaredField("mSelectionDivider"); pf.setAccessible(true); pf.set(picker, new ColorDrawable(color)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } }