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

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

问题描述

我遇到与此主题相同的问题: 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 programmatically.

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

This is the code I am using but is not working

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

我该如何才能使它工作?

how can I make it work?

推荐答案

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文件,将其放在resources文件夹中,并将以下内容粘贴为其内容:

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

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