问题描述
我想在对话框中更改DatePicker的蓝色分隔线的颜色。这只是一个带有DatePicker和ButtonBar的正常DialogFragment。
I'm trying to change color of the blue dividers for a DatePicker in a dialog. This is just a normal DialogFragment with a DatePicker and a ButtonBar.
有没有人知道更改这些分隔符,或者如果没有替换整个DatePicker, ?
Does anyone know to change these dividers, or if it's even possible without replacing the entire DatePicker with a custom one?
Mini rant
现在我看到太多的回答提示以下代码:
Now I've seen too many answers suggesting the following code:
<style name="datePickerTheme" parent="@android:style/Widget.DeviceDefault.DatePicker">
<item name="android:divider">**your @drawable/ or @color/ here**</item>
</style>
这根本不起作用。你有人试过这个建议这段代码吗?
Which simply does not work. Have you guys tried this before suggesting this code? It should work perfectly, but it does not seem to work with the DatePicker.
推荐答案
以下内容可以用来完成,但
The following approach worked for me.This sets divider colours for all fields (also for am/pm)
private void applyStyLing(TimePickerDialog timePickerDialog){
Resources system = Resources.getSystem();
int hourNumberPickerId = system.getIdentifier("hour", "id", "android");
int minuteNumberPickerId = system.getIdentifier("minute", "id", "android");
int ampmNumberPickerId = system.getIdentifier("amPm", "id", "android");
NumberPicker hourNumberPicker = (NumberPicker) timePickerDialog.findViewById(hourNumberPickerId);
NumberPicker minuteNumberPicker = (NumberPicker) timePickerDialog.findViewById(minuteNumberPickerId);
NumberPicker ampmNumberPicker = (NumberPicker) timePickerDialog.findViewById(ampmNumberPickerId);
setNumberPickerDividerColour(hourNumberPicker);
setNumberPickerDividerColour(minuteNumberPicker);
setNumberPickerDividerColour(ampmNumberPicker);
}
private void setNumberPickerDividerColour(NumberPicker number_picker){
final int count = number_picker.getChildCount();
for(int i = 0; i < count; i++){
try{
Field dividerField = number_picker.getClass().getDeclaredField("mSelectionDivider");
dividerField.setAccessible(true);
ColorDrawable colorDrawable = new ColorDrawable(mContext.getResources().getColor(R.color
.interactive_color));
dividerField.set(number_picker,colorDrawable);
number_picker.invalidate();
}
catch(NoSuchFieldException e){
Log.w("setNumberPickerTxtClr", e);
}
catch(IllegalAccessException e){
Log.w("setNumberPickerTxtClr", e);
}
catch(IllegalArgumentException e){
Log.w("setNumberPickerTxtClr", e);
}
}
}
这篇关于Android分隔符颜色DatePicker对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!