0
我試圖在我用startActivityForResult調用的活動中顯示datePicker和timePicker小部件。這兩個選擇器應該匹配父級佈局寬度。在我的情況下,它適用於timePicker,但datePicker不會填滿整個屏幕。Fullscreen DatePicker inside活動
我試着以編程方式測量顯示尺寸並在onResume裏面設置了datePicker的layoutparams。但不幸的是,它沒有奏效。
如何實現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>
的onResume在DateTimePickerAct方法ivity:
/* ... */
@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(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); – KrishnaJ