DatePicker和TimePicker的大小被google写死了 在xml中限定大小 更本没用
1.创建一个类 继承FrameLayout ,然后修改FrameLayout的onmeasure方法.
public class MyWrapContentView extends FrameLayout {
public MyWrapContentView(Context context) {
super(context);
}
public MyWrapContentView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyWrapContentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.AT_MOST,0);//核心代码
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
2.使用该控件包裹DatePicker和TimePicker
<cn.schope.lrr.view.MyWrapContentView
android:layout_gravity="center" #此处必需设置居中,外层是Relativelayout就用centerinparent,是LinearLayout便用layoutgravity
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_gravity="center"
android:id="@+id/ll_picker"
android:layout_width="wrap_content"#使用包裹体
android:layout_height="wrap_content"
android:orientation="horizontal">
<DatePicker
android:id="@+id/dp_submit_day"
android:layout_width="wrap_content"#使用包裹体
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TimePicker
android:layout_width="wrap_content"#使用包裹体
android:layout_height="wrap_content"
android:id="@+id/time_picker"/>
</LinearLayout>
</cn.schope.lrr.view.MyWrapContentView>
3.使用Nineoldandroids开源库中的ViewHelper缩放控件.(V4包的ViewCompat也可)
View ll_picker = view.findViewById(R.id.ll_picker);
ViewHelper.setScaleX(ll_picker,0.8f);//可以随意指定缩小百分比
ViewHelper.setScaleY(ll_picker,0.8f);
如此就可以实现DatePicker和TimePicker横向并列显示了.