由于 Android SDK 不允许您在 timepicker 中修改元素。你需要一种方法来进行黑客攻击。
对于文本颜色,您需要更改主题。这是我的片段中的一个小例子。
重点是“R.style.Picker_Black”,我将主题设置为“Theme.Holo.Light”,以便在时间选择器中显示“黑色”文本颜色。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Picker_Black);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View v = localInflater.inflate(R.layout.fragment_booking, container, false);
return v;
}
在“style.xml”中定义样式
<style name="Picker.Black" parent="android:Theme.Holo.Light">
<item name="android:editTextStyle">@style/Widget.EditText.White</item>
</style>
对于分频器:
public class CustomTimePicker extends TimePicker {
public CustomTimePicker(Context context) {
super(context);
init();
}
public CustomTimePicker(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTimePicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
removeAllDividers(this);
}
private void removeAllDividers(ViewGroup picker) {
for (int i = 0; i < picker.getChildCount(); i++) {
View view = picker.getChildAt(i);
if (view instanceof NumberPicker) {
try {
Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
field.setAccessible(true);
field.set(view, null);
} catch (Exception e) {
}
} else if (view instanceof ViewGroup) {
removeAllDividers((ViewGroup) view);
}
}
}
}