DialogFragment 中的 DatePicker 忽略 setCalendarViewShown

DatePicker in a DialogFragment ignores setCalendarViewShown

我想在 DialogFragment 中显示 DatePicker:

public class DatePickerDialogFragment extends DialogFragment {
    private OnDateSetListener dateSetListener = null;
    private String title = null;

    public DatePickerDialogFragment() {}

    public DatePickerDialogFragment(OnDateSetListener dateSetListener, String title) {
        this.dateSetListener = dateSetListener;
        this.title = title;
    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this.getActivity(), this.dateSetListener, year, month, day);
        datePickerDialog.getDatePicker().setCalendarViewShown(false);
        datePickerDialog.setTitle(this.title);

        return datePickerDialog;
    }
}

不幸的是datePickerDialog.getDatePicker().setCalendarViewShown(false);被完全忽略了。我讨厌日历视图,因为它又丑又不舒服。那么我该如何禁用它呢?

在我的 "values-v21/styles.xml" 中,我修改了 activity 主题,如下所示,它隐藏了日历并显示了简单的日期选择器微调器。您可能想要使用具有白色背景和圆角的可绘制对象:

    <item name="android:datePickerDialogTheme">@style/style_date_picker_dialog</item>
</style>


<style name="style_date_picker_dialog" parent="@android:style/Theme.DeviceDefault.Light">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowBackground">@drawable/dialog_background</item>
    <item name="android:datePickerStyle">@style/style_datepicker</item>
</style>

<style name="style_datepicker" parent="android:Widget.Material.Light.DatePicker">
    <item name="android:datePickerMode">spinner</item>
</style>

这已在其他地方得到回答:Android Material Design Inline Datepicker issue,并试图用已接受的答案解决它,但我无处可去。

基本上你需要做的:

<DatePicker
 ...
 android:datePickerMode="spinner" />

因为 setCalandarShown(); 已被弃用。