更改 NumberPicker 分隔线颜色 [英] Changing NumberPicker divider color
问题描述
在最近的 android 版本中, 但在运行时出现错误(见下文)由于库中的某些代码试图访问不存在的资源 ID.
android.content.res.Resources$NotFoundException: 资源 ID #0x0在 android.content.res.Resources.getValue(Resources.java:1123)在 android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)在 android.content.res.Resources.getLayout(Resources.java:939)在 android.view.LayoutInflater.inflate(LayoutInflater.java:395)在 net.simonvt.numberpicker.NumberPicker.(NumberPicker.java:635)在 net.simonvt.numberpicker.NumberPicker.(NumberPicker.java:560)在 net.simonvt.numberpicker.NumberPicker.(NumberPicker.java:550)
基于此 (https://stackoverflow.com/a/20291416/2915480 虽然是关于 DatePicker)有几种方法:
- 在没有 mSelectionDivider 及其附属公司的情况下编写您自己的 NumberPicker,或者使用由 Vikram 反向移植.最后一种情况:
- 从 github 从 lib 下载
- 更改 res/drawable-xxx/np_numberpicker_selection_divider.9.png 中的 drawable:
- 透明(或其他).9.png* 在 res/drawable 中创建 np_numberpicker_selection_divider.xml 形状线资源(使用
0dp
高度或透明颜色).
OR 从 NumberPicker.java 像 这里
使用反射访问
private final field mSelectionDivider
(详情:https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/NumberPicker.java) - 例如请参阅此处的修改.我使用了反射,但这不是最好的解决方案.在 API 21+ 中使用主题覆盖数字选择器的分隔线颜色:
?attr/colorControlNormal
确定材料数字选择器中分隔线的颜色,因此在您的小部件主题中更改此颜色会做的伎俩,例如对于日期选择器:
在小部件中:
On recent android versions, number pickers use a blue divider when drawn (cf. image below).
I would like to change this color. Is there a working solution? or perhaps a library that package an updated version of NumberPicker that allows customizing the divider color?
I have tried android-numberpicker but I get an error (see below) at runtime due to some code from the library that tries to access to a resource id that does not exist.
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1123)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
at android.content.res.Resources.getLayout(Resources.java:939)
at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
at net.simonvt.numberpicker.NumberPicker.<init>(NumberPicker.java:635)
at net.simonvt.numberpicker.NumberPicker.<init>(NumberPicker.java:560)
at net.simonvt.numberpicker.NumberPicker.<init>(NumberPicker.java:550)
Based on this (https://stackoverflow.com/a/20291416/2915480 although it's about DatePicker) there are several ways:
- Write your own NumberPicker without mSelectionDivider and its affiliates or use backported by Vikram. In last case:
- download from lib from github
- change drawable in res/drawable-xxx/np_numberpicker_selection_divider.9.png:
- to transparent (or whatever) .9.png
* create np_numberpicker_selection_divider.xml shape line resource in res/drawable (with
0dp
height or transparent color).
OR remove
if (mSelectionDivider != null)
branch from onDraw(Canvas) method in NumberPicker.java like hereUse reflection to access
private final field mSelectionDivider
(details: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/NumberPicker.java) - e.g. see modification here. I used reflection but it's not the best solution.Use theming to override the number picker's divider color in API 21+:
?attr/colorControlNormal
determines the color of the divider in material number picker, so changing this color in your widget's theme will do the trick, e.g. for DatePicker:
<style name="MyAppTheme.NumberPicker" parent=" MyAppTheme">
<item name="android:colorControlNormal"> ?colorAccent </item>
</style>
and in the widget:
<DatePicker
android:id="@+id/question_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:gravity="center"
android:theme="@style/MyAppTheme.NumberPicker" />
这篇关于更改 NumberPicker 分隔线颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!