【问题标题】:Changing NumberPicker divider color not working with Android Api 29+更改 NumberPicker 分隔线颜色不适用于 Android Api 29+
【发布时间】:2021-03-14 13:24:29
【问题描述】:

我正在使用以下代码更改NumberPicker 分隔线颜色, 此代码不适用于 Android Api 29+,因为 UnsupportedAppUsage mSelectionDivider

private static void setDividerColor(NumberPicker picker, int color) {
    java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
    for (java.lang.reflect.Field pf : pickerFields) {
      if (pf.getName().equals("mSelectionDivider")) {
        pf.setAccessible(true);
        try {
          ColorDrawable colorDrawable = new ColorDrawable(color);
          pf.set(picker, colorDrawable);
        } catch (IllegalArgumentException | Resources.NotFoundException | IllegalAccessException e) {
          e.printStackTrace();
        }
        break;
      }
    }
}

【问题讨论】:

  • 创建自己的小部件,而不是尝试使用反射来修改框架小部件。

标签: android divider numberpicker


【解决方案1】:

您可以像这样使用number-picker 库:

implementation 'com.shawnlin:number-picker:2.4.11'

在xml中:

<com.shawnlin.numberpicker.NumberPicker
    android:id="@+id/color_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:np_dividerColor="#000000"/>

在java中设置颜色:

numberPicker.setDividerColor(Color);

【讨论】:

    【解决方案2】:

    将此样式设置为 android:theme` 用于数字选择器:

    <style name="AppTheme.NumberPickerTheme">
            <item name="android:textColorSecondary">@color/yourDividerColor</item>
        </style>
    

    <NumberPicker
            ...
            android:theme="@style/AppTheme.NumberPickerTheme"
           />
    

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2014-08-05
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 2021-11-09
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多