我尝试在我使用startActivityForResult调用的活动中显示datePicker和timePicker小部件。两个选择器都应与父布局宽度匹配。在我的情况下,它适用于timePicker,但datePicker不会填满整个屏幕。
我尝试以编程方式测量显示大小,并在onResume中设置datePicker的layoutparams。但不幸的是,它没有用。
如何实现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);
}
/* ... */
答案 0 :(得分:0)
试一下
<强> Style.xml 强>
<style name="DialogTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
MainActivity.java 将主题应用于日期选择器的构造函数(R.style.AppTheme)
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.AppTheme, this, year, month, day);
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return datePickerDialog;