【问题标题】:Android 6.0 DatePickerDialog ThemeAndroid 6.0 DatePickerDialog 主题
【发布时间】:2015-11-17 21:33:49
【问题描述】:

似乎任何使用 Marshmallow (Android 6.0) 的人都无法在我的应用程序中使用 DatePicketDialog。我似乎遇到了某种主题问题。我使用包含DatePicketDialogDialogFragment 供用户选择生日。这是使用 Android 5.x 和 d 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, month, 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>

【问题讨论】:

  • 请提供minimal, complete, and verifiable example 证明您的问题。在这种情况下,这将包括有关托管此片段的活动所使用的主题的详细信息,以及来自片段本身的代码。在 Android 6.0 上只需显示 DatePickerDialog 即可。
  • 用代码更新了帖子。
  • “但如果我想得对,那不会触及 DialogFragment 吗?” -- 不,但它可能会影响DatePickerDialog 本身,因为DatePickerDialog 扩展了AlertDialog
  • 我添加了CustomDialogTheme代码
  • 嗯...左侧的屏幕截图看起来不像以 Holo 为主题的 DatePickerDialog,它仍然使用旧的向上/向下“微调器”方法来选择日期,而不是显示日历。然而你的 CustomDialogTheme 正在使用 Holo。我无法解释。

标签: android themes dialogfragment datepickerdialog


【解决方案1】:

您在styles.xml 中创建了主题,但没有将其引用到DatePickerDialog 之类的

DatePickerDialog dlg = new DatePickerDialog(getActivity(),
                R.style.datepickerCustom,this,year,month,day);

并创建一个文件夹名称values-v21 并将style.xml 的副本添加到其中。并粘贴

<style name="datepickerCustom" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
        <item name="android:colorAccent">@color/orange_theme</item>
        <item name="android:windowBackground">@android:color/darker_gray</item>
        <!--<item name="android:windowContentOverlay">@null</item>-->
        <!--<item name="android:textColorPrimary">@color/white</item>-->
        <!--<item name="android:textColorSecondary">@color/white</item>-->
    </style>

【讨论】:

  • 对于 naugat 我必须创建什么文件夹名称
  • values-v24 将是 Naugat 文件夹的名称
【解决方案2】:

只需在 styles.xml 中更改 colorAscent 颜色:

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="colorAccent">#FC6C2D</item>   ---> I used orange color here to display the picker dialog in orange color for marshmallow device.

    <item name="android:textColorPrimary">#000000</item>
    <item name="android:textColorSecondary">#000000</item>

</style>

【讨论】:

    【解决方案3】:

    感谢帖子How to change the style of Date picker in android?,我能够正确显示DatePicker。在设置DatePicker 样式时,我必须使用parent="Theme.AppCompat.Light.Dialog"

    【讨论】:

    • 如果我使用您的解决方案,我的 DatePicker 的主题正在发生变化(旧主题,如 4.4)。有什么建议???
    猜你喜欢
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多