2017-09-12 87 views
0

我試圖在我用startActivityForResult調用的活動中顯示datePicker和timePicker小部件。這兩個選擇器應該匹配父級佈局寬度。在我的情況下,它適用於timePicker,但datePicker不會填滿整個屏幕。Fullscreen DatePicker inside活動

我試着以編程方式測量顯示尺寸並在onResume裏面設置了datePicker的layoutparams。但不幸的是,它沒有奏效。

如何實現datePicker小部件,使其延伸至父佈局的寬度?

日期選擇器屏幕截圖

enter image description here

timePicker截圖

enter image description here

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); 
} 
/* ... */ 
+0

可能是你應該是這樣的: imageView.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); – KrishnaJ

回答

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;