【问题标题】:How to Change Time Picker Font Color in Android?如何在 Android 中更改时间选择器字体颜色?
【发布时间】:2015-10-08 17:49:50
【问题描述】:

您好,我想更改时间选择器的字体颜色。我在 Stackoverflow 本身中搜索了其他一些答案。但我无法帮助自己解决这个问题。

我的目标 API 是 19。这是我尝试过的事情的列表。

  1. 我尝试在 values-v11 和 values-v14 文件夹中的 Styles.xml 中添加样式。以下是代码

<!-- language: lang-xml --> <style name="MyTimePicker" parent="@android:style/Widget.Holo.DatePicker"> <item name="android:textColor">@color/main_font</item> </style>
我也尝试将它添加到值文件夹中。但我得到以下错误。

@android:style/Widget.Holo.DatePicker 需要 API 级别 11(当前最低为 8)

我在我的 TimePicker 中添加了样式,例如

<TimePicker
        android:id="@+id/alarm_timePicker"
        style="@style/MyTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

什么都没有改变。

2 。我尝试在我的活动中添加一些来自 Stackoverflow 的代码。以下是代码。

public static boolean setNumberPickerTextColor(TimePicker numberPicker) {
    final int count = numberPicker.getChildCount();
    int color = R.color.main_font;
    for (int i = 0; i < count; i++) {
      View child = numberPicker.getChildAt(i);
      if (child instanceof EditText) {
        try {
          Field selectorWheelPaintField =
              numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");
          selectorWheelPaintField.setAccessible(true);
          ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
          ((EditText) child).setTextColor(color);
          numberPicker.invalidate();
          return true;
        } catch (NoSuchFieldException e) {
          //Log.w("setNumberPickerTextColor", e);
        } catch (IllegalAccessException e) {
          //Log.w("setNumberPickerTextColor", e);
        } catch (IllegalArgumentException e) {
          //Log.w("setNumberPickerTextColor", e);
        }
      }
    }
    return false;
    }

当我尝试调试它时,将内容视图设置为时出现错误

尝试在空对象引用上调用虚拟方法“void android.widget.NumberPicker.setOnValueChangedListener(android.widget.NumberPicker$OnValueChangeListener)”

【问题讨论】:

    标签: android android-styles android-timepicker


    【解决方案1】:

    我犯了同样的错误。我想您自己找到了答案,但对于其他人:尝试将您的父样式从 @android:style/Widget.Holo.DatePicker 更改为 @android:style/Widget.Holo.TimePicker

    @android:style/Widget.Material.Light.DatePicker 使用相同的技巧,将其更改为 @android:style/Widget.Material.Light.TimePicker

    【讨论】:

      【解决方案2】:

      文本颜色是主题的 textColorPrimary 和 textColorSecondary 值。所以你可以创建这样的样式:

      <style name="myTheme" parent="Theme.AppCompat.NoActionBar">
      
          <item name="android:textColorPrimary">#e90d8a</item>
          <item name="android:textColorSecondary">@color/white_primary_text</item>
      
         // The rest of your customization.
      
      </style>
      

      在您的活动中使用 setTheme 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-26
        • 1970-01-01
        • 2013-12-16
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多