如何从默认白色更改DatePickerDialog的背景颜色?

时间:2017-12-16 17:54:26

标签: android android-datepicker datepickerdialog

我正在尝试为Android中的DatePickerDialog设置黑暗主题样式。根据{{​​3}},我不想在DatePickerDilog's构造函数中设置样式。

我设置了以下样式&它已经设定但不适用于OK&在纵向模式下取消按钮的面板。但是,它适用于Landscape !!:

我的v21 / styles.xml:

<style name="AppTheme.Dark.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
     <item name="android:windowDrawsSystemBarBackgrounds">true</item>
     <item name="android:statusBarColor">@color/primaryColorDark_dark</item>
     <item name="android:datePickerDialogTheme">@style/DatePicker_Theme_Dark</item>
</style>
<!--Dark DatePicker Theme & style-->
<style name="DatePicker_Theme_Dark" parent="android:Theme.Material.Light.Dialog">
    <item name="android:datePickerStyle">@style/DatePicker_Dark</item>
</style>
<style name="DatePicker_Dark" parent="@android:style/Widget.Material.Light.DatePicker">
    <item name="colorAccent">@color/colorAccent_dark</item>
    <item name="android:background">@color/primaryColor_dark</item>
    <item name="android:headerBackground">@color/colorAccent_dark</item>
    <item name="android:textColorPrimary">@color/textPrimaryColor_dark</item>
    <item name="android:textColorSecondary">@color/textSecondaryColor_dark</item>
</style>

我的styles.xml:

<!-- Dark Application theme -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowActionModeOverlay">false</item>
</style>
<style name="AppTheme.Dark" parent="AppTheme.Base">
    <item name="android:windowBackground">@color/backgroundColor_dark</item>
    <item name="colorAccent">@color/colorAccent_dark</item>
    <item name="android:textColorSecondary">@color/textSecondaryColor_dark</item>
    <item name="android:textColorPrimary">@color/textPrimaryColor_dark</item>
    <item name="android:datePickerStyle">@style/DatePicker_Dark</item>
</style>

<style name="DatePicker_Dark" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/colorAccent_dark</item>
    <item name="android:background">@color/primaryColor_dark</item>
    <item name="android:headerBackground">@color/colorAccent_dark</item>
    <item name="android:textColorPrimary">@color/textPrimaryColor_dark</item>
    <item name="android:textColorSecondary">@color/textSecondaryColor_dark</item>
</style>

在我的.java文件中,我将获得DatePickerDialog的新实例,如下所示:

return new DatePickerDialog(getActivity(), this, year, month, day){
    @Override
    public void onDateChanged(@NonNull DatePicker view, int year, int month, int dayOfMonth) {
          onDateSet(view, year, month, dayOfMonth);
          dismiss();
    }
 };

快照: Vikram's answer here

我们将不胜感激您的帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

DatePicker颜色是父主题的选择。

<!-- Theme.AppCompat.Light is not a dialog theme -->
<style name="DialogTheme" parent="**Theme.AppCompat.Light**">
    <item name="colorAccent">@color/your_color</item>
</style>

并在DatePickerDialog()中使用上述主题。

new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        //DO SOMETHING
    }
}, 2015, 02, 26).show();

快乐的编码!!

答案 1 :(得分:0)

我发现更改按钮栏工具栏颜色的唯一方法是传递@StyleRes

AlertDialog.THEME_TRADITIONAL
AlertDialog.THEME_HOLO_DARK
AlertDialog.THEME_HOLO_LIGHT
AlertDialog.THEME_DEVICE_DEFAULT_DARK
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT

当实例化DatePickerDialog时。此外,更改主题属性可能会接受自定义颜色,但我没有进行检查。对我来说,仅是将其更改为@ color / colorPrimary就足够了,例如下面的示例。希望这会有所帮助。

DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), AlertDialog.THEME_DEVICE_DEFAULT_DARK, dateSetListener, year,month,day);