在 API 21 中以编程方式从 DatePicker 中隐藏 Calendarview [英] Hide Calendarview from DatePicker programmatically in API 21

查看:14
本文介绍了在 API 21 中以编程方式从 DatePicker 中隐藏 Calendarview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与此线程相同的问题:Android Material Design Inline Datepicker issue,但我没有使用 XML 布局,而是以编程方式构建 DatePicker.

I'm having the same issue as this thread: Android Material Design Inline Datepicker issue, but I am not using an XML layout, instead I'm building the DatePicker programmatically.

这是我正在使用但不起作用的代码

This is the code I am using but is not working

DatePicker dpView = new DatePicker(ctx);
dpView.setCalendarViewShown(false);
dpView.setSpinnersShown(true);

我怎样才能让它工作?

推荐答案

Android 5.0 的问题是决定是否使用日历的模式"是在构造时读取的,而在代码中无法设置直到之后的模式已经被构建,因此为时已晚.(来源在这里:DatePicker 源代码)

The problem in Android 5.0 is the "mode" which determines whether to use a calendar or not is read at construct time, and in code you can't set the mode until after it has been constructed, thus it's too late. (Source is here: DatePicker Source Code)

我的解决方案是创建我自己的可重复使用的 DatePicker 布局,指定无日历"模式,并使用该布局而不是 Android 的默认布局以编程方式构建我的 DateTimes.

My solution was to create my own reusable DatePicker layout that specifies the "no calendar" mode, and construct my DateTimes programmatically with that layout instead of Android's default.

最重要的是,创建一个DatePicker.axml"文件,将其放入资源文件夹中,粘贴以下内容作为其内容:

Bottom line is, create a "DatePicker.axml" file, put it in the resources folder, and paste the following as its contents:

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:calendarViewShown="false"
          android:datePickerMode="spinner"/>

并在任何你需要的代码中声明它,如下所示:

and declare it wherever you need in code like this:

LayoutInflater inflater = LayoutInflater.From( Activity );
DatePicker datePicker = (DatePicker)inflater.Inflate( Resource.Layout.DatePicker, null );

这篇关于在 API 21 中以编程方式从 DatePicker 中隐藏 Calendarview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆