Android 6.0 DatePickerDialog主题

前端之家收集整理的这篇文章主要介绍了Android 6.0 DatePickerDialog主题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
似乎任何使用Marshmallow( Android 6.0)的人都无法在我的应用程序中使用DatePicketDialog.似乎遇到了某种我遇到的主题问题.我使用DialogFragment,它包含一个DatePicketDialog供用户选择生日.以下是Android 5.x和6.x的DialogFragment镜头.


我试图在DatePickerDialog构造函数添加一个主题,但这使得DialogFragment全屏,我不希望这样.有谁知道我怎么能让DatePickerDialog看起来像是在Marshmallow之前?

更新1

这是我创建DialogFragment的代码

DialogFragment ageFragment = new DatePickerDialogFragment();
ageFragment.show(getFragmentManager(),"datePicker");

这是DatePickerDialogFragment中的onCreateDialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker if no filters are set
    Calendar cal = Calendar.getInstance();
    // Set the date 18 years prevIoUs,since only 18 and older are allowed on the app
    cal.add(Calendar.YEAR,-18);
    int year,month,day;
    if (iDialogListener.getYear() == -1 || iDialogListener.getMonth() == -1
            || iDialogListener.getDay() == -1) {
        Calendar defaultCal = Calendar.getInstance();
        // 40 is the default age to show
        defaultCal.add(Calendar.YEAR,-40);
        year = defaultCal.get(Calendar.YEAR);
        month = defaultCal.get(Calendar.MONTH);
        day = defaultCal.get(Calendar.DAY_OF_MONTH);
    } else {
        year = iDialogListener.getYear();
        month = iDialogListener.getMonth();
        day = iDialogListener.getDay();
    }

    DatePickerDialog datepicker = new DatePickerDialog(getActivity(),this,year,day);
    datepicker.getDatePicker().setMaxDate(cal.getTimeInMillis());
    Calendar minDate = Calendar.getInstance();
    minDate.set(Calendar.YEAR,Calendar.getInstance().get(Calendar.YEAR) - 100);
    datepicker.getDatePicker().setMinDate(minDate.getTimeInMillis());

    // Create a new instance of DatePickerDialog and return it
    return datepicker;
}

在themes.xml中,唯一触及Dialogs的行是

<item name="android:alertDialogTheme">@style/CustomDialogTheme</item>

但是,如果我正在思考,那不会触及DialogFragment吗?

更新2

这是CustomDialogTheme:

<style name="CustomDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

解决方法

感谢 How to change the style of Date picker in android?后,我能够正确显示DatePicker.在为DatePicker设置样式时,我不得不使用parent =“Theme.AppCompat.Light.Dialog”
原文链接:https://www.f2er.com/android/313225.html

猜你在找的Android相关文章