java 在 API 21 中以编程方式从 DatePicker 中隐藏 Calendarview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27709668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide Calendarview from DatePicker programmatically in API 21
提问by ludwiguer
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.
我遇到了与此线程相同的问题:Android Material Design Inline Datepicker issue,但我没有使用 XML 布局,而是以编程方式构建 DatePicker。
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?
我怎样才能让它工作?
回答by Luismi
If you have to set it up programmatically in a DatePickerFragment just set this:
如果您必须在 DatePickerFragment 中以编程方式设置它,只需设置:
DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Dialog_MinWidth, this, year, month, day);
with whatever style you like.
搭配你喜欢的任何风格。
回答by JHawkZZ
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 afterit has been constructed, thus it's too late. (Source is here: DatePicker Source Code)
Android 5.0的问题是在构造时读取决定是否使用日历的“模式”,而在代码中你不能在构造完成之后设置模式,因此为时已晚。(来源在这里:DatePicker 源代码)
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 布局,指定“无日历”模式,并使用该布局而不是 Android 的默认布局以编程方式构建我的 DateTimes。
Bottom line is, create a "DatePicker.axml" file, put it in the resources folder, and paste the following as its contents:
最重要的是,创建一个“DatePicker.axml”文件,将其放在资源文件夹中,然后粘贴以下内容作为其内容:
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="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 );
回答by tharkay
Adding to the answerfrom user Luismi:
The theme Theme.Holo.Dialog
or Theme.Holo.Dialog.MinWidth
might cause problems like two boxes drawn around the date picker.
主题Theme.Holo.Dialog
或Theme.Holo.Dialog.MinWidth
可能会导致问题,例如围绕日期选择器绘制的两个框。
Instead you should the theme Theme.Holo.Dialog.NoFrame
to prevent this. The theme might not be accessable from your code, so just create your own theme:
相反,您应该使用主题Theme.Holo.Dialog.NoFrame
来防止这种情况。您的代码可能无法访问该主题,因此只需创建您自己的主题:
<style name="DatePickerDialogTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
Edit: The better way to fix this problem is to use the SpinnerDatePicker librarythat recreates the old design.
编辑:解决此问题的更好方法是使用重新创建旧设计的SpinnerDatePicker 库。
回答by Fernando Bonet
I simply changed my AppTheme(v21) style and worked:
我只是改变了我的 AppTheme(v21) 风格并工作:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorPrimary</item>
<item name="android:timePickerDialogTheme">@style/PickerDialogCustom</item>
<item name="android:datePickerDialogTheme">@style/PickerDialogCustom</item>
<item name="alertDialogTheme">@style/AlertDialogCustom</item>
</style>
<style name="PickerDialogCustom" parent="AlertDialogCustom">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:textColorPrimary">@color/colorPrimaryDark</item>
<item name="colorControlNormal">@color/greyLight500</item>
<item name="android:layout_margin">2dp</item>
<item name="android:datePickerMode">spinner</item>
</style>
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:positiveButtonText">@color/colorPrimary</item>
<item name="android:negativeButtonText">@color/greyDark200</item>
<item name="buttonBarNegativeButtonStyle">@style/negativeButton</item>
<item name="android:datePickerStyle">@style/PickerDialogCustom</item>
</style>