似乎使用Marshmallow(Android 6.0)的任何人都无法在我的应用中使用DatePicketDialog
。似乎存在某种我遇到的主题问题。我使用包含DialogFragment
的{{1}}供用户选择生日。以下是Android 5.x和6.x的DialogFragment镜头。
我尝试在DatePicketDialog
构造函数中添加主题,但这使得DatePickerDialog
全屏,我不想要这样。有谁知道我怎么能让DialogFragment
看起来像棉花糖之前的那样?
更新1
以下是我创建DatePickerDialog
的代码:
DialogFragment
以下是DialogFragment ageFragment = new DatePickerDialogFragment();
ageFragment.show(getFragmentManager(), "datePicker");
中的onCreateDialog
:
DatePickerDialogFragment
在themes.xml中,唯一触及@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;
}
的行是
Dialogs
但是,如果我正确思考,那不会触及<item name="android:alertDialogTheme">@style/CustomDialogTheme</item>
吗?
更新2
以下是DialogFragment
:
CustomDialogTheme
答案 0 :(得分:4)
感谢帖子How to change the style of Date picker in android?,我能够正确展示DatePicker
。在设置parent="Theme.AppCompat.Light.Dialog"
DatePicker
答案 1 :(得分:4)
只需更改样式 .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>
答案 2 :(得分:4)
您在
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>