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

我正在尝试为
Android中的DatePickerDialog设置Dark Theme Style.根据
Vikram’s answer here,我不想在DatePickerDilog的构造函数中设置样式.

我设置了以下样式&它已经设定但不适用于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();
    }
 };

快照:
《android – 如何从默认的白色更改DatePickerDialog的背景颜色?》

我们将非常感谢您的帮助.谢谢!

最佳答案 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();

快乐的编码!!

点赞