【发布时间】:2015-10-08 17:49:50
【问题描述】:
您好,我想更改时间选择器的字体颜色。我在 Stackoverflow 本身中搜索了其他一些答案。但我无法帮助自己解决这个问题。
我的目标 API 是 19。这是我尝试过的事情的列表。
- 我尝试在 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