方式一
修改Picker的字体颜色的方法,就是指自定义TimePicker的样式
<TimePicker
android:id="@+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:theme="@style/Theme.picker"
android:timePickerMode="spinner" />
在style.xml下面加入下面代码:
<style name="Theme.picker" parent="android:Theme.Holo.NoActionBar">
<item name="android:editTextStyle">@style/Widget.EditText.White</item>
</style>
<style name="Widget.EditText.White" parent="@android:style/Widget.EditText">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">30sp</item>
</style>
方式二:通过工具类修改
public class TimePickerUIUtil {
private static final String TAG = TimePickerUIUtil.class.getSimpleName();
public static void set_timepicker_text_colour(TimePicker time_picker,Context context) {
Resources system = Resources.getSystem();
int hour_numberpicker_id = system.getIdentifier("hour", "id", "android");
int minute_numberpicker_id = system.getIdentifier("minute", "id", "android");
int ampm_numberpicker_id = system.getIdentifier("amPm", "id", "android");
NumberPicker hour_numberpicker = (NumberPicker) time_picker.findViewById(hour_numberpicker_id);
NumberPicker minute_numberpicker = (NumberPicker) time_picker.findViewById(minute_numberpicker_id);
NumberPicker ampm_numberpicker = (NumberPicker) time_picker.findViewById(ampm_numberpicker_id);
set_numberpicker_text_colour(hour_numberpicker,context);
set_numberpicker_text_colour(minute_numberpicker,context);
set_numberpicker_text_colour(ampm_numberpicker,context);
setNumberPickerDividerColor(hour_numberpicker, Color.rgb(255,255,255));
setNumberPickerDividerColor(minute_numberpicker,Color.rgb(255,255,255));
setNumberPickerDividerColor(ampm_numberpicker,Color.rgb(255,255,255));
setPickerSize(hour_numberpicker,70,context);
setPickerSize(minute_numberpicker,70,context);
setPickerSize(ampm_numberpicker,70,context);
}
private static void set_numberpicker_text_colour(NumberPicker number_picker, Context context) {
final int count = number_picker.getChildCount();
//这里就是要设置的颜色,修改一下作为参数传入会更好
final int color = ContextCompat.getColor(context, R.color.white);
for (int i = 0; i < count; i++) {
View child = number_picker.getChildAt(i);
try {
Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint) wheelpaint_field.get(number_picker)).setColor(color);
((EditText) child).setTextColor(color);
number_picker.invalidate();
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
Log.i(TAG, "set_numberpicker_text_colour: "+e);
}
}
}
private static void setNumberPickerDividerColor(NumberPicker numberPicker, int color) {
Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (Field SelectionDividerField : pickerFields) {
if (SelectionDividerField.getName().equals("mSelectionDivider")) {
SelectionDividerField.setAccessible(true);
try {
SelectionDividerField.set(numberPicker, new ColorDrawable(color));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
//此方法将dp值转换为px值,以保证适配不同分辨率机型
private static int dp2px(Context context, float dpVal)
{
retur n (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
//这个方法是改变NumberPicker大小的方法,传入的参数为要修 改的NumberPicker和NumberPicker的宽度值
private static void setPickerSize(NumberPicker np, int widthDpValue,Context context) {
int widthPxValue = dp2px(context, widthDpValue);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthPxValue, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);//这儿参数可根据需要进行更改
np.setLayoutParams(params);
}
private static List<NumberPicker> findNumberPicker(ViewGroup viewGroup)
{
List<NumberPicker> npList = new ArrayList<NumberPicker>();
View child = null;
if (null != viewGroup)
{
for (int i = 0; i < viewGroup.getChildCount(); i++)
{
child = viewGroup.getChildAt(i);
if (child instanceof NumberPicker)
{
npList.add((NumberPicker)child);
}
else if (child instanceof LinearLayout)
{
List<NumberPicker> result = findNumberPicker((ViewGroup)child);
if (result.size() > 0)
{
return result;
}
}
}
}
return npList;
}
private static EditText findEditText(NumberPicker np)
{
if (null != np)
{
for (int i = 0; i < np.getChildCount(); i++)
{
View child = np.getChildAt(i);
if (child instanceof EditText)
{
return (EditText)child;
}
}
}
return null;
}
public static void setNumberPickerTextSize(ViewGroup viewGroup)
{
List<NumberPicker> npList = findNumberPicker(viewGroup);
if (null != npList)
{
for (NumberPicker np : npList)
{
EditText et = findEditText(np);
et.setFocusable(false);
et.setGravity(Gravity.CENTER);
et.setTextSize(30);
}
}
}
}