更改DatePicker背景颜色

时间:2018-01-20 17:39:00

标签: android datepicker

我正在尝试在另一个活动之上显示DatePicker对话框,发生的事情是它以某种方式继承了它的颜色。

我希望它有绿色标题和白色背景, like on this example

以下是样式

的摘录
<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);
            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();

However it instead is displayed in all green

如果我在样式中指定了白色背景,it overrides the header and the buttons as well

    <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() 

But it'd still display the whole dialog in green

以下是我从

打开对话框的活动样式
<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>

我正在使用的颜色

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

有谁知道如何完成它?我很感激任何指导。我试图关注this answer, but had no luck

3 个答案:

答案 0 :(得分:5)

此代码适合我试试这个......

styles.xml

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

弹出代码

        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();

enter image description here

答案 1 :(得分:0)

要在应用程序级别更改DatePicker颜色(日历模式),请定义以下属性。

<style name="MyAppTheme" parent="Theme.AppCompat.Light">
    <item name="colorAccent">#ff6d00</item>
    <item name="colorControlActivated">#33691e</item>
    <item name="android:selectableItemBackgroundBorderless">@color/colorPrimaryDark</item>
    <item name="colorControlHighlight">#d50000</item>
</style>

参考:How to change the style of a DatePicker in android?

答案 2 :(得分:0)

以下自定义DatePickerDoalog类不仅可以自定义暗色,而且还可以使暗淡的动画显示

/**
 * @author Taras Yurkiv @Devlight
 */
public class DatePickerDialogCustomDim extends DatePickerDialog {

    private final long animDuration = 100;
    private float dimAmount = 0.7f;

    private Drawable dimDrawable;
    private ViewGroup root;

    private OnDismissListener outsideDismissListener;

    private final OnDismissListener dismissListener = new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            final ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                    PropertyValuesHolder.ofInt("alpha", (int) (255 * dimAmount), 0));
            animator.setTarget(dimDrawable);
            animator.setDuration(animDuration);
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    ViewGroupOverlay overlay = root.getOverlay();
                    overlay.remove(dimDrawable);
                }
            });
            animator.start();
            if (outsideDismissListener != null)
                outsideDismissListener.onDismiss(dialog);
        }
    };


    @TargetApi(Build.VERSION_CODES.N)
    public DatePickerDialogCustomDim(@NonNull Context context) {
        this(context, 0);
    }

    @TargetApi(Build.VERSION_CODES.N)
    public DatePickerDialogCustomDim(@NonNull Context context, @StyleRes int themeResId) {
        this(context, themeResId, null, -1, -1, -1);
        init(context);
    }

    public DatePickerDialogCustomDim(@NonNull Context context,
                                     @Nullable OnDateSetListener listener,
                                     int year,
                                     int month,
                                     int dayOfMonth) {
        this(context, 0, listener, year, month, dayOfMonth);
    }

    public DatePickerDialogCustomDim(@NonNull Context context,
                                     @StyleRes int themeResId,
                                     @Nullable OnDateSetListener listener,
                                     int year,
                                     int monthOfYear,
                                     int dayOfMonth) {
        super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
        init(context);
    }

    private void init(Context context) {
        root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
        super.setOnDismissListener(dismissListener);
    }

    public void setDimAmount(@FloatRange(from = 0, to = 1f) float dim) {
        dimAmount = dim;
    }

    @Override
    public void show() {
        super.show();
        dimDrawable = new ColorDrawable(Color.WHITE); // a dim color
        dimDrawable.setBounds(0, 0, root.getWidth(), root.getHeight());

        ViewGroupOverlay overlay = root.getOverlay();
        overlay.add(dimDrawable);

        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                PropertyValuesHolder.ofInt("alpha", 0, (int) (255 * dimAmount)));
        animator.setTarget(dimDrawable);
        animator.setDuration(animDuration);
        animator.start();
    }

    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        outsideDismissListener = listener;
    }
}

它与一种样式结合使用(主要是禁用暗淡)

<style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/accent</item>
    <item name="android:textColorLink">@color/primary</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>