Android:如何更改datepicker分隔线的颜色?

我的Android应用程序中有一个dateselect器,但是现在我想将蓝色分隔线的颜色更改为绿色(请参阅下面的图片)。 还有一些关于Stackoverflow的讨论也是一样的,但都没有给出一个解决scheme的答案。

所以我去找我自己,发现实际上是一个android:datePickerStyle,还有一个android:divider。 不过,我不知道,分频器是否实际上是指dateselect器中的分频器。 我尝试了两种组合,但我似乎没有得到它的工作。 所以我的第一个问题: android:divider是否指向datepicker中的分隔线,我怎样才能使用它来改变颜色?

所以另一种select是创build一个全新的自定义dateselect器。 如果这让我只是改变分隔线的颜色,我为此而失望。 所以我看了一些关于创build自定义dateselect器的 教程 ,但是他们都没有定义分隔符的颜色。 分隔符不在xml文件或java文件中列出。

这将是很好,如果有一些样板代码来重新创builddateselect器,因为它目前显示,包括设置分隔符的颜色的代码。 希望这将使我能够复制它,只需改变分配器的颜色设置。 所以我的第二个问题是: 有人会知道任何简单地实现dateselect器的样板代码(包括分隔符的定义)吗?

在这里输入图像描述

不幸的是,这不是一个微不足道的任务。

DatePickers内部使用小部件NumberPickerCalendarView 。 例如,您发布的图片使用3个NumberPickers 。 而你所说的分隔符来自NumberPicker的属性: selectionDivider 。 问题是这个属性是不公开的,也没有numberPickerStyle ,通过它,这个属性被设置。

我最近将CalendarView和NumberPicker移植到了API 8,主要是为了好玩。 由于代码是可用的(在Android的源代码中查找android.widget.NumberPicker和其他人),所有这些任务需要时间,并且通过android的源代码进行挖掘。 例子:

  1. 简单==>你将不得不从View类更改私人variables的访问方法

    mLeft(View类中的受保护variables)==> getLeft()(public访问器方法)

  2. 最耗时的任务是恢复辅助function方法。

在任何情况下,如果您决定编写DatePicker的自定义实现,则必须为NumberPicker和CalendarView(可选)编写它们。

更简单的方法:

Backport DatePicker可在此处作为库: Android-DatePicker 。 如上所述,您将使用backport的CalendarView和NumberPicker与此DatePicker一起使用。

你需要改变的是:

使用{library-numberpicker} / res / drawable-xxxx / np_numberpicker_selection_divider.9.png作为模板,并将“偏蓝”颜色更改为绿色(我使用pixlr )。 如果您想完全使用蓝色分隔线,或者使用不同的名称并在{library-numberpicker} / res / values / themes.xml进行更改,则可以使用相同的名称进行{library-numberpicker} / res / values / themes.xml

如果您select不同的名称,那么在themes.xml需要进行更改:

 <style name="NPWidget.Holo.NumberPicker" parent="NPWidget.NumberPicker"> .... <item name="selectionDivider">@drawable/new_nine_path_drawable_name</item> .... </style> 

就是这样。

使用库的输出:

在这里输入图像描述

编辑:

android:divider是否指向datepicker中的divider,我怎样才能用它来改变颜色?

属性divider实际上来自LinearLayoutNumberPickerinheritance了这个属性,因为NumberPicker extends LinearLayout 。 但是这个divider服务于不同的目的。 传递给该属性的drawable放置在LinearLayout子视图之间。

属性android:showDividers用于更改此分隔符的位置,可能的值为:

  • none:没有显示分隔符
  • 开头:分隔符显示在第一个子视图之前
  • 中间:分隔符显示在每个子视图之后,而不是在最后一个子视图之后
  • 结束:分隔符显示在最后一个子视图之后

属性android:dividerPadding是不言自明的。

即使NumberPickerinheritance这个属性,它也不会使用它。 从您自己的研究和试验中可以看出这一点: I tried a multitude of combinations of the two, but I don't seem to get it to work.

要查看divider属性,请执行以下操作:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:divider="@android:drawable/ic_media_play" android:showDividers="middle" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="World," /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Again" /> </LinearLayout> 

使用javareflection的Hack-ish解决方法:

这个答案给了我这个想法。 我讨厌使用一般的反应,主要是这个答案中列出的原因: 链接 。 尽pipe我在此完整列出,但我build议您不要使用它。

 public class CDP extends android.widget.DatePicker { public CDP(Context context, AttributeSet attrs) { super(context, attrs); Class<?> internalRID = null; try { internalRID = Class.forName("com.android.internal.R$id"); } catch (ClassNotFoundException e) { e.printStackTrace(); } Field month = null; try { month = internalRID.getField("month"); } catch (NoSuchFieldException e) { e.printStackTrace(); } NumberPicker npMonth = null; try { npMonth = (NumberPicker) findViewById(month.getInt(null)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } Field day = null; try { day = internalRID.getField("day"); } catch (NoSuchFieldException e) { e.printStackTrace(); } NumberPicker npDay = null; try { npDay = (NumberPicker) findViewById(day.getInt(null)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } Field year = null; try { year = internalRID.getField("year"); } catch (NoSuchFieldException e) { e.printStackTrace(); } NumberPicker npYear = null; try { npYear = (NumberPicker) findViewById(year.getInt(null)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } Class<?> numberPickerClass = null; try { numberPickerClass = Class.forName("android.widget.NumberPicker"); } catch (ClassNotFoundException e) { e.printStackTrace(); } Field selectionDivider = null; try { selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider"); } catch (NoSuchFieldException e) { e.printStackTrace(); } try { selectionDivider.setAccessible(true); selectionDivider.set(npMonth, getResources().getDrawable( R.drawable.np_numberpicker_selection_divider_green)); selectionDivider.set(npDay, getResources().getDrawable( R.drawable.np_numberpicker_selection_divider_green)); selectionDivider.set(npYear, getResources().getDrawable( R.drawable.np_numberpicker_selection_divider_green)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } 

我们在这里做什么:

  • 延长DatePicker
  • 如果您在sdk/platforms/android-xx/res/layout打开了date_picker.xml ,那么您将看到三个NumberPickers的id, monthdayyear 。 我们访问android.internal.R.id获取这些NumberPickers的资源ID。
  • 我们使用findViewById(int)方法创build三个NumberPicker对象。
  • 然后,使用mSelectionDivider访问和检索Field mSelectionDivider
  • 将字段设置为可访问(如其声明的final),使用Field#set(Object, Object)方法设置其值。 第一个参数是我们执行这个操作的Object。 第二个参数是我们要设置的对象。

我用过的drawable可以从这里下载。

我认为最简单的解决scheme可能是使用样式。

把这个放在你的styles.xml文件中

  <!-- changes the default colours for EditTexts, including non-text elements (also works with the DatePicker --> <style name="appCompatStyle" parent="Theme.AppCompat.Light"> <item name="colorControlNormal">@color/lightPrimaryText</item> <item name="colorControlActivated">@color/colorAccent</item> <item name="android:editTextStyle">@style/editTextStyle</item> </style> <!-- changes the default text colour for the EditTexts --> <style name="editTextStyle" parent="android:style/Widget.EditText"> <item name="android:textColor">@color/lightPrimaryText</item> </style> 

并将这些属性放在布局XML中

 android:theme="@style/appCompatStyle" 

并自定义它,但是你喜欢。

有一个图书馆,他们有时间和dateselect器。 在那里你可以按照方式改变分隔线的颜色

  timePicker.setSelectionDivider(new ColorDrawable(0xffff0000)); timePicker.setSelectionDividerHeight(2); 

编辑另一个库,我也使用过,也可以使用下面的方法在.ms.xml中进行自定义

 <style name="NPWidget.Holo.NumberPicker" parent="NPWidget.NumberPicker"> <item name="solidColor">@android:color/transparent</item> <item name="selectionDivider">@color/div_color</item> <item name="selectionDividerHeight">0.3dip</item> <item name="internalLayout">@layout/number_picker_with_selector_wheel</item> <item name="internalMinWidth">64dip</item> <item name="internalMaxHeight">140dip</item> <item name="virtualButtonPressedDrawable">@drawable/item_background_holo_dark</item> </style> 

首先; 维克拉姆,你做得很好,谢谢你的努力! 有一件事,我在net.simonvt.datepicker.DatePickerDialog中缺less的是设置titleDivider颜色,我想与numberPickerDividerColor匹配的numberPickerDividerColor 。 所以我在Vikrams实现中添加了这个选项,并在这里发布。 这是与更改AlertDialog.titleDividerColor相关的常见解决scheme。 也许它会帮助别人。

 class net.simonvt.datepicker.DatePickerDialog private int titleDividerColor; 

显示对话框时设置颜色很重要,所以我在onAttachedToWindow方法中做这个。

 @Override public void onAttachedToWindow() { super.onAttachedToWindow(); if (titleDividerColor <= 0) { return; } try { int dividerId = getContext().getResources().getIdentifier("android:id/titleDivider", null, null); View divider = findViewById(dividerId); if (divider != null) { divider.setBackgroundColor(getContext().getResources().getColor(titleDividerColor)); } } catch (Exception e) {} } public void setTitleDividerColor(int titleDividerColor) { this.titleDividerColor = titleDividerColor; } public int getTitleDividerColor() { return titleDividerColor; } 

编辑

我在这里发布了另一个解决schemehttps://stackoverflow.com/a/33800696/1134335

为了改变DatePickerDialog中的分隔线颜色,在你的主题中改变android:datePickerDialogTheme风格就足够了:

主题风格:

  <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:datePickerDialogTheme">@style/style_date_picker_dialog</item> </style> 

对话风格:

 <style name="style_date_picker_dialog" parent="AppCompatAlertDialogStyle"> <item name="colorControlNormal">@color/colorAccent</item> </style> 

从Android分频器颜色DateRicker对话框的 清障车

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

 <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. />