如何更改DatePicker对话框的颜色

时间:2013-04-10 13:12:38

标签: android

我想更改DatePicker对话框的颜色。我将对话框加载为

@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");
}

加载对话框时,它是白色背景。如何更改显示第二张图片中显示的颜色? 感谢enter image description here enter image description here

2 个答案:

答案 0 :(得分:6)

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

首先,在值中添加一个这样的themes.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+)的目录,并放置一个这样的themes.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>  

For More Details查看该链接并按照该链接进行操作。

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

祝你好运。

答案 1 :(得分:3)

这是我会尝试的。对于Android&lt; = 2.3(API 10 / v10),让您的主题从Android 3.0(API 11 / v11)中的默认Android光主题扩展,我将从holo light主题扩展。你可以在这里看到如何做到这一点: How to use Holo.Light theme, and fall back to 'Light' on pre-honeycomb devices?

我不是100%肯定会改变你的警告对话框,因为我没有广泛使用灯光主题,所以你可能需要从灯光主题中编辑一两个属性来获得编辑文本的背景要轻松。