4

我喜欢更改 DatePicker Dialog 的颜色。我将对话框加载为

@SuppressLint("NewApi")
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

       @SuppressLint("NewApi")
    @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
            return new DatePickerDialog(getActivity(), this, year, month, day);
      }

      public void onDateSet(DatePicker view, int year, int month, int day) {
           // Do something with the date chosen by the user


      }


}

@SuppressLint("NewApi")
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");//show(getSupportFragmentManager(), "datePicker");
}

当它加载对话框时,它是白色背景。如何更改显示颜色,如第二张图片所示?谢谢在此处输入图像描述在此处输入图像描述

4

2 回答 2

6

根据您的查询,您必须创建自定义对话框主题并在custom_theme.xmlNow 中设置,只需根据您的API版本进行设置。

首先,在值中添加一个像这样的主题.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <!-- Any customizations for your app running on pre-3.0 devices here -->
    </style>
</resources> 

然后,在 res 目录中创建一个名为“values-v11”(Android 3.0+)的目录,并像这样放置一个 theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
        <!-- Any customizations for your app running on 3.0+ devices here -->
    </style>
</resources>

最后在res目录下创建一个名为“values-v14”(Android 4.0+)的目录,并创建一个themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
        <!-- Any customizations for your app running on 4.0+ devices here -->
    </style>
</resources>  

有关更多详细信息,请查看链接并关注它。

希望你能从他们那里得到一些想法并解决你的问题。

祝你好运。

于 2013-05-04T10:47:18.020 回答
3

这是我会尝试的。对于 Android <= 2.3 (API 10 / v10),让您的主题从默认的 android light 主题扩展,在 Android 3.0 (API 11 / v11) 及更高版本中,我将从 holo light 主题扩展。你可以在这里看到如何做到这一点: 如何使用 Holo.Light 主题,并在预蜂窝设备上回退到“Light”?

我不是 100% 确定这会改变您的警报对话框,因为我没有广泛使用浅色主题,因此您可能需要从浅色主题中编辑一个或两个属性以使编辑文本的背景变浅.

于 2013-04-10T14:32:36.000 回答