Hide Calendarview from DatePicker programmatically in API 21

后端 未结 4 652
说谎
说谎 2020-12-29 17:42

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 programmat

相关标签:
4条回答
  • 2020-12-29 17:53

    Adding to the answer from user Luismi:

    The theme Theme.Holo.Dialog or Theme.Holo.Dialog.MinWidth might cause problems like two boxes drawn around the date picker.

    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:

    <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 library that recreates the old design.

    0 讨论(0)
  • 2020-12-29 17:55

    I simply changed my AppTheme(v21) style and worked:

    <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>
    
    0 讨论(0)
  • 2020-12-29 18:09

    If you have to set it up programmatically in a DatePickerFragment just set this:

    DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Dialog_MinWidth, this, year, month, day);
    

    with whatever style you like.

    0 讨论(0)
  • 2020-12-29 18:11

    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)

    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.

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

    <?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 );
    
    0 讨论(0)
提交回复
热议问题