【发布时间】:2017-09-12 11:36:09
【问题描述】:
我试图在我使用 startActivityForResult 调用的活动中显示 datePicker 和 timePicker 小部件。两个选择器都应该匹配父布局宽度。在我的情况下,它可以与 timePicker 一起使用,但 datePicker 不会填满整个屏幕。
我尝试以编程方式测量显示大小并在 onResume 中设置 datePicker 的布局参数。但不幸的是,它没有奏效。
如何实现 datePicker 小部件,使其延伸到父布局的宽度?
datePicker 截图
timePicker 截图
activity_date_time_picker.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
DateTimePickerActivity 中的onResume 方法:
/* ... */
@Override
protected void onResume() {
super.onResume();
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) datePicker.getLayoutParams();
params.width = width;
datePicker.setLayoutParams(params);
}
/* ... */
【问题讨论】:
-
可能你应该是这样的:imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
标签: java android xml datepicker android-datepicker