android – DialogFragment中的DatePicker忽略setCalendarViewShown

前端之家收集整理的这篇文章主要介绍了android – DialogFragment中的DatePicker忽略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”中,我修改了活动主题,如下所示隐藏日历&显示简单的datepicker微调器.您可能想要使用带有白色背景的可绘制的&圆角:
<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相关文章