问题描述
如何在Appcompat主题中选择的数字之间更改TimePicker线的颜色?线是蓝色的,但我需要橙色的线.
How to change TimePicker lines color between choosed number in Appcompat theme? The lines are blue, but i need orange lines.
我将TimePickerDialog与ContextThemeWrapper一起使用.
I use TimePickerDialog with ContextThemeWrapper.
TimePickerDialog timePicker = new TimePickerDialog(
new ContextThemeWrapper(getActivity(), R.style.timePicker),
this, hour, minute, DateFormat.is24HourFormat(getActivity()));
和样式
<style name="timePicker">
<item name="android:divider">@drawable/cab_background_top_play</item>
</style>
,但该行可能还有其他ID.我不确定只能使用drawable还是color.
but the line have other id maybe. I'm not sure to use drawable or color only.
预先感谢
推荐答案
我用自己的Timepicker类解决了这个问题,该类扩展了Android Timepicker类.我的灵感来自帖子 stackoverflow.com/Questions/20148671/android-how-to-to-to-change-the-color-of-datepicker-divider/20291416#20291416 其中发布了在我的询问下方的评论中.
I solved this with own Timepicker class which extends android Timepicker class. I inspired by post stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider/20291416#20291416 which posted mohammad rababah in comment below my ask.
以下是样式化的时间选择器类:
Here is styled timepicker class:
public class StyledTimePicker extends TimePicker {
public StyledTimePicker(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("hour");
} 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("minute");
} 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("amPm");
} 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.color.apptheme_color));
selectionDivider.set(npDay, getResources().getDrawable(
R.color.apptheme_color));
selectionDivider.set(npYear, getResources().getDrawable(
R.color.apptheme_color));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
您只能更改颜色apptheme_color.
You can change color apptheme_color only.
这篇关于如何在Appcompat主题中更改TimePicker线条的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!