对于 Holo TimePicker,您可以使用它。
public static void setTimePickerHeightTopBottomMargin(
TimePicker timePicker, int height, int topMargin, int bottomMargin) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
try {
Field f;
View v;
ViewGroup.MarginLayoutParams params;
// mHourSpinner
f = TimePicker.class.getDeclaredField("mHourSpinner");
f.setAccessible(true);
v = (View) f.get(timePicker);
params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
params.height = height;
params.topMargin = topMargin;
params.bottomMargin = bottomMargin;
// mMinuteSpinner
f = TimePicker.class.getDeclaredField("mMinuteSpinner");
f.setAccessible(true);
v = (View) f.get(timePicker);
params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
params.height = height;
params.topMargin = topMargin;
params.bottomMargin = bottomMargin;
// mAmPmSpinner
f = TimePicker.class.getDeclaredField("mAmPmSpinner");
f.setAccessible(true);
v = (View) f.get(timePicker);
params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
params.height = height;
params.topMargin = topMargin;
params.bottomMargin = bottomMargin;
} catch (Exception e) {
Log.w(TAG, e);
}
}
}
单位是像素,因此请确保从 dp 转换。不要使用小于 120dp 的高度。
保持高度并移除垂直边距通常就足够了:
setTimePickerHeightTopBottomMargin(mTimePicker,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0);
虽然我没有测试过它,但 DatePicker 应该是相同的,而是使用对 fields 的反射mDaySpinner
,mMonthSpinner
而mYearSpinner
不是。