本文介绍了无法以编程方式让我的 DatePickerDialog 使用微调样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DialogFragment 打开一个 DatePickerDialog

I'm using a DialogFragment to open a DatePickerDialog

public class DatePickerFragment extends DialogFragment{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);


    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog DatePickerDialog = new DatePickerDialog(getActivity(), (ProfileCreationActivity)getActivity(), year, month, day);
    return DatePickerDialog;
}

我想要日历外观,但我更喜欢微调外观.

I'm getting a calendar look, where I would prefer a spinner look.

我试过了:

datePickerDialog.getDatePicker().setCalendarViewShown(false);

datePickerDialog.getDatePicker().setLayoutMode(1);

但它不起作用.

请注意,我希望微调器查找一项活动,但我希望查看另一项活动的日历视图.所以我不能改变整个应用程序的风格.我需要为一项活动自定义样式.

Please note that I want the spinner look for one activity, but that I will want the calendar view for another activity. So I can not change the whole application style. I need a custom style for one activity.

推荐答案

您可以以编程方式保留它,不需要使用微调器创建新的 XML,我只是更改了我的 AppTheme(v21) 样式并工作;-)

You can keep it programatically, don't need to create a new XML with the spinner, 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>

记得保持对 =21 时忽略此命令

remember to keep the support for <21 just adding this line, this command is ignored for >=21

datePickerDialog.getDatePicker().setLayoutMode(1);

这篇关于无法以编程方式让我的 DatePickerDialog 使用微调样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:17