在API 21中以DatePicker方式隐藏Calenderview [英] Hide Calenderview from DatePicker programatically in API 21

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

问题描述

我有与此主题相同的问题: Android Material Design Inline Datepicker问题,但是我没有使用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 programatically.

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

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布局,指定无日历模式,构建我的DateTimes以编程方式使用该版式,而不是Android的默认值。

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:


<?xml version =1.0encoding =utf-8?>

<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方式隐藏Calenderview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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