在MaterialCalandarView中更改当前日期颜色

时间:2017-06-15 13:14:45

标签: android colors android-widget android-calendar calendarview

我正在使用MaterialCalendarView。这是代码:

<com.prolificinteractive.materialcalendarview.MaterialCalendarView
    android:id="@+id/calendar_view"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_margin="@dimen/standard_space"
    android:layout_weight="1"
    app:mcv_dateTextAppearance="@dimen/very_small_text_size"
    app:mcv_showOtherDates="all"/>

我想给当前日期设置黑色边界,我该怎么做?

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果要设置所有选定日期的颜色,则必须将此行添加到xml中:

 app:mcv_selectionColor="@color/yourColor"

并选择当前日期:

calendarView.setDateSelected(Calendar.getInstance(),true);

如果您只想为当前日期设置不同的颜色,则必须将Decorator添加到MaterialCalendarView:

 calendarView.addDecorator(new DayViewDecorator() {
        @Override
        public boolean shouldDecorate(CalendarDay day) {
            Calendar cal1 = day.getCalendar();
            Calendar cal2 = Calendar.getInstance();

            return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
                    && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                    && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setBackgroundDrawable(ContextCompat.getDrawable(MainActivity.this,R.drawable.selector));
        }
    });

其中selector是您的自定义selector.xml,例如:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_shortAnimTime">

<item
    android:state_checked="true"
    android:drawable="@color/yourColor"
    />

<item
    android:state_pressed="true"
    android:drawable="@color/yourColor"
    />

<item android:drawable="@android:color/transparent" />

选择器参考链接:https://github.com/prolificinteractive/material-calendarview/blob/master/docs/CUSTOM_SELECTORS.md

装饰者参考链接:https://github.com/prolificinteractive/material-calendarview/blob/master/docs/DECORATORS.md