Android分隔线颜色DatePicker对话框

我试图在对话框中更改DatePicker的蓝色分隔线的颜色。 这只是一个带有DatePicker和ButtonBar的普通DialogFragment。

有谁知道要更改这些分隔线,或者如果甚至可能没有用自定义的replace整个DatePicker?

截图DatePicker

迷你咆哮

现在我已经看到了太多的答案,提示下面的代码:

<style name="datePickerTheme" parent="@android:style/Widget.DeviceDefault.DatePicker"> <item name="android:divider">**your @drawable/ or @color/ here**</item> </style> 

这根本不起作用。 有没有人build议这个代码之前,尝试过这个? 它应该完美的工作,但它似乎不适用于DatePicker。

Solutions Collecting From Web of "Android分隔线颜色DatePicker对话框"

下面的方法为我工作。这设置所有领域的分隔线颜色(也为上午/下午)

  private void applyStyLing(TimePickerDialog timePickerDialog){ Resources system = Resources.getSystem(); int hourNumberPickerId = system.getIdentifier("hour", "id", "android"); int minuteNumberPickerId = system.getIdentifier("minute", "id", "android"); int ampmNumberPickerId = system.getIdentifier("amPm", "id", "android"); NumberPicker hourNumberPicker = (NumberPicker) timePickerDialog.findViewById(hourNumberPickerId); NumberPicker minuteNumberPicker = (NumberPicker) timePickerDialog.findViewById(minuteNumberPickerId); NumberPicker ampmNumberPicker = (NumberPicker) timePickerDialog.findViewById(ampmNumberPickerId); setNumberPickerDividerColour(hourNumberPicker); setNumberPickerDividerColour(minuteNumberPicker); setNumberPickerDividerColour(ampmNumberPicker); } private void setNumberPickerDividerColour(NumberPicker number_picker){ final int count = number_picker.getChildCount(); for(int i = 0; i < count; i++){ try{ Field dividerField = number_picker.getClass().getDeclaredField("mSelectionDivider"); dividerField.setAccessible(true); ColorDrawable colorDrawable = new ColorDrawable(mContext.getResources().getColor(R.color .interactive_color)); dividerField.set(number_picker,colorDrawable); number_picker.invalidate(); } catch(NoSuchFieldException e){ Log.w("setNumberPickerTxtClr", e); } catch(IllegalAccessException e){ Log.w("setNumberPickerTxtClr", e); } catch(IllegalArgumentException e){ Log.w("setNumberPickerTxtClr", e); } } } 

我通过这样做解决了这个问题:

我通过查找“ mSelectionDivider ”来改变DatePicker分隔符。 然后我有问题的标题divider看起来很愚蠢,所以我添加了一个textview上面的LinearLayout包含3个datepickers和使用newFragment.setTitle(""); 删除原来的一个。

分隔线可绘制的例子: 向做这个的家伙致谢! 🙂 http://ge.tt/8wK7TZ71/v/0?c

结果图片 < – 我的结果

例:

  public DatePickerDialog makeDatePicker(OnDateSetListener listener, Calendar cal) { Calendar c; if (cal == null) { c = Calendar.getInstance(); } else { c = cal; } int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); DatePickerDialog newFragment = new DatePickerDialog(this, listener, year, month, day); // removes the original topbar: newFragment.setTitle(""); // Divider changing: DatePicker dpView = newFragment.getDatePicker(); LinearLayout llFirst = (LinearLayout) dpView.getChildAt(0); LinearLayout llSecond = (LinearLayout) llFirst.getChildAt(0); for (int i = 0; i < llSecond.getChildCount(); i++) { NumberPicker picker = (NumberPicker) llSecond.getChildAt(i); // Numberpickers in llSecond // reflection - picker.setDividerDrawable(divider); << didn't seem to work. Field[] pickerFields = NumberPicker.class.getDeclaredFields(); for (Field pf : pickerFields) { if (pf.getName().equals("mSelectionDivider")) { pf.setAccessible(true); try { pf.set(picker, getResources().getDrawable(R.drawable.np_numberpicker_selection_divider_orange)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } } // New top: int titleHeight = 90; // Container: LinearLayout llTitleBar = new LinearLayout(this); llTitleBar.setOrientation(LinearLayout.VERTICAL); llTitleBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, titleHeight)); // TextView Title: TextView tvTitle = new TextView(this); tvTitle.setText("Select a date"); tvTitle.setGravity(Gravity.CENTER); tvTitle.setPadding(10, 10, 10, 10); tvTitle.setTextSize(24); tvTitle.setTextColor(Color.BLACK); tvTitle.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, titleHeight-2)); llTitleBar.addView(tvTitle); // View line: View vTitleDivider = new View(this); vTitleDivider.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2)); vTitleDivider.setBackgroundColor(getResources().getColor(R.color.crumblrOrange)); llTitleBar.addView(vTitleDivider); dpView.addView(llTitleBar); FrameLayout.LayoutParams lp = (android.widget.FrameLayout.LayoutParams) llFirst.getLayoutParams(); lp.setMargins(0, titleHeight, 0, 0); return newFragment; } 

我基于Ajit的答案这个代码,但我把它调整为DatePicker而不是TimePicker 。 而且,为了保持安全,我添加了一个null检查:

 public static void colorizeDatePicker(DatePicker datePicker) { Resources system = Resources.getSystem(); int dayId = system.getIdentifier("day", "id", "android"); int monthId = system.getIdentifier("month", "id", "android"); int yearId = system.getIdentifier("year", "id", "android"); NumberPicker dayPicker = (NumberPicker) datePicker.findViewById(dayId); NumberPicker monthPicker = (NumberPicker) datePicker.findViewById(monthId); NumberPicker yearPicker = (NumberPicker) datePicker.findViewById(yearId); setDividerColor(dayPicker); setDividerColor(monthPicker); setDividerColor(yearPicker); } private static void setDividerColor(NumberPicker picker) { if (picker == null) return; final int count = picker.getChildCount(); for (int i = 0; i < count; i++) { try { Field dividerField = picker.getClass().getDeclaredField("mSelectionDivider"); dividerField.setAccessible(true); ColorDrawable colorDrawable = new ColorDrawable(picker.getResources().getColor(R.color.colorAccent)); dividerField.set(picker, colorDrawable); picker.invalidate(); } catch (Exception e) { Log.w("setDividerColor", e); } } } 

产量

有色的分隔符

考虑到:

你不需要一个新的R.drawable.divider

你可以写:

  pf.set(number_picker, new ColorDrawable(getResources().getColor(R.color.red))); 

这解决了我的问题,下面的代码添加到应用程序的主题风格。

 <item name="colorControlNormal">@color/colorAccent</item> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorControlNormal">@color/blue</item> </style> 

当然可以。 您可以使用这些属性作为例子:

 <NumberPicker android:layout_width="wrap_content" android:layout_height="wrap_content" selectionDivider="@color/black" //The divider for making the selection area selectionDividerHeight="1px"//The height of the selection divider selectionDividersDistance="3dp"//The distance between the two selection dividers internalLayout="@layout/something"//The layout of the number picker. internalMaxHeight="5dp"//The max height of the NumberPicker (also check other variations) internalMinWidth="5dp" // The max width of the NumberPicker (also check other variations) virtualButtonPressedDrawable="@drawable/something"//The drawable for pressed virtual (increment/decrement) buttons. /> 

更新:

你可以使用这个自定义dateselect器 。 这是高度可定制的和落后的竞争力。 它基本上使用numberpicker,你可以使用上面的属性设置divider。

感谢Mvj解决问题! 只是以为我也会在时间select器的分隔线上显示我的解决scheme。

  TimePickerDialog ptd = new TimePickerDialog(getActivity(), R.style.PickerStyler, this, hour, minute,DateFormat.is24HourFormat(getActivity())); ptd.setTitle(""); //Needs to be try catched Field mTimePickerField = ptd.getClass().getDeclaredField("mTimePicker"); mTimePickerField.setAccessible(true); TimePicker mTimePickerInstance = (TimePicker) mTimePickerField.get(ptd); LinearLayout llFirst = (LinearLayout) mTimePickerInstance.getChildAt(0); LinearLayout llSecond = (LinearLayout) llFirst.getChildAt(0); boolean continiue = false; NumberPicker picker = null; for (int i = 0; i < llSecond.getChildCount(); i++) { continiue = true; try{picker = (NumberPicker) llSecond.getChildAt(i); }catch(Exception e){continiue = false;} if(continiue){ Field[] pickerFields = NumberPicker.class.getDeclaredFields(); for (Field pf : pickerFields) { if (pf.getName().equals("mSelectionDivider")) { pf.setAccessible(true); try { pf.set(picker, getResources().getDrawable(R.drawable.divider)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } } } 

此版本针对DatePicker的特定用途进行了调整。 虽然Andrea Lazzarotto的版本工作正常,但它使用了一个不必要的循环,导致了多种颜色变化。 除了微小的代码改进,我的版本使用应用程序主题的主要颜色来匹配分隔线颜色(请参阅我如何从我的应用程序主题获取主要颜色? )。 使用Android 6.0和8.0进行testing:

 private void colorizeDatePicker(final DatePicker datePicker) { final Resources system = Resources.getSystem(); final String defType = "id"; final String defPackage = "android"; final int dayId = system.getIdentifier("day", defType, defPackage); final int monthId = system.getIdentifier("month", defType, defPackage); final int yearId = system.getIdentifier("year", defType, defPackage); final NumberPicker dayPicker = (NumberPicker) datePicker.findViewById(dayId); final NumberPicker monthPicker = (NumberPicker) datePicker.findViewById(monthId); final NumberPicker yearPicker = (NumberPicker) datePicker.findViewById(yearId); setDividerColor(dayPicker); setDividerColor(monthPicker); setDividerColor(yearPicker); } private void setDividerColor(final NumberPicker picker) { if (picker == null) { return; } try { final Field dividerField = picker.getClass().getDeclaredField("mSelectionDivider"); dividerField.setAccessible(true); final TypedValue outValue = new TypedValue(); getContext().getTheme().resolveAttribute(R.attr.colorPrimary, outValue, true); final int dividerColor = outValue.data; dividerField.set(picker, new ColorDrawable(dividerColor)); picker.invalidate(); } catch (Exception e) { } }