更改DatePicker背景颜色 [英] Change DatePicker background color

查看:461
本文介绍了更改DatePicker背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在另一个活动的顶部显示一个 DatePicker 对话框,所发生的是它以某种方式继承了它的颜色。



我希望它具有绿色的标头和白色背景,



此处摘录自样式

 < style name = DatePickerDialog parent = @ android:style / Theme.Holo.Light> 
< item name = android:windowFrame> @ null< / item>
< item name = android:windowIsFloating> true< / item>
< item name = android:windowIsTranslucent> true< / item>
< item name = colorAccent> @ color / primary< / item>
< / style>

此代码用于弹出 DatePicker

  DatePickerDialog datepicker = new DatePickerDialog(this,R.style.DatePickerDialog,new DatePickerDialog.OnDateSetListener(){

public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth){
TextView newdate =(TextView)findViewById(R.id.newdate);
日期日期= getDate( year,monthOfYear,dayOfMonth);
DateFormat dateformat = new SimpleDateFormat(getResources()。getString(R.string.date_format_full));
newdate.setText(dateformat.format(date));
}
},newCalendar.get(Calendar.YEAR),newCalendar.get(Calendar.MONTH),newCalendar.get(Calendar.DAY_OF_MONTH));

datepicker.show();



如果我在样式中指定白色背景,则

 < item name = android:background> @ color / app_background< / item> 

我尝试的最后一件事是使用 AlertDialog.THEME_DEVICE_DEFAULT_DARK 作为 DatePicker 主题

  DatePickerDialog datepicker = new DatePickerDialog(this,
AlertDialog.THEME_DEVICE_DEFAULT_DARK,new
DatePickerDialog.OnDateSetListener()



这是我从以下位置打开对话框的活动的样式

 < style name = UserDialog parent =  android:style / Theme.Dialog> 
< item name = android:windowFrame> @ null< / item>
< item name = android:windowIsFloating> true< ; / item>
< item name = android:windowIsTranslucent> true< / item>
< item name = android:background> @ color / primary< / item>
< item name = android:textColor> @ color / dialog_text < / item>
< item name = colorPrimary> @ color / app_background< / item>
< item name = android:windowTitleStyle> @ style / NewDialogTitle< / item>
< / style>

< style name = NewDialogTitle parent = @ android:style / TextAppearance.DialogWindowTitle>
< item name = android:gravity> center_horizo​​ntal< / item>
< / style>

我正在使用的颜色

 <颜色名称= primary>#4CAF50< / color> 
< color name = app_background>#FFFFFF< / color>

有人知道如何完成吗?如有任何指导,我将不胜感激。我试图遵循


I am trying to display a DatePicker dialog on top of another activity and what is happening is it is somehow inheriting its color.

I'd like it to have a green header and white background,

Here is excerpt from styles

<style name="DatePickerDialog" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorAccent">@color/primary</item>
</style>

And this code is used to pop up the DatePicker

    DatePickerDialog datepicker = new DatePickerDialog(this, R.style.DatePickerDialog, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            TextView newdate = (TextView) findViewById(R.id.newdate);
            Date date = getDate(year, monthOfYear, dayOfMonth);
            DateFormat dateformat = new SimpleDateFormat(getResources().getString(R.string.date_format_full));
            newdate.setText(dateformat.format(date));
        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    datepicker.show();

If I specify the white background in the styles,

    <item name="android:background">@color/app_background</item>

One last thing I've tried is to use AlertDialog.THEME_DEVICE_DEFAULT_DARK as the DatePicker theme

DatePickerDialog datepicker = new DatePickerDialog(this, 
AlertDialog.THEME_DEVICE_DEFAULT_DARK, new 
DatePickerDialog.OnDateSetListener() 

Here is the style of the activity I am opening the dialog from

<style name="UserDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:background">@color/primary</item>
    <item name="android:textColor">@color/dialog_text</item>
    <item name="colorPrimary">@color/app_background</item>
    <item name="android:windowTitleStyle">@style/NewDialogTitle</item>
</style>

<style name="NewDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
    <item name="android:gravity">center_horizontal</item>
</style>

And the colors I am using

<color name="primary">#4CAF50</color>
<color name="app_background">#FFFFFF</color>

Does anyone know how to get it done? I'd appreciate any guidance. I've tried to follow this answer, but had no luck

解决方案

This code worked for me try this ...

styles.xml

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@android:color/holo_green_dark</item>
</style>

Popup Code

        Calendar mcurrentDate = Calendar.getInstance();
        int mYear = mcurrentDate.get(Calendar.YEAR);
        int mMonth = mcurrentDate.get(Calendar.MONTH);
        int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog mDatePicker;
        mDatePicker = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {

                Toast.makeText(context,"Selected Date " + + selectedday + "-" + ++selectedmonth  + "-" + selectedyear ,Toast.LENGTH_SHORT).show();
            }
        }, mYear, mMonth, mDay);
        mDatePicker.show();

这篇关于更改DatePicker背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆