我如何在CalendarView中应用样式/颜色到目前为止

时间:2016-05-09 12:42:33

标签: android calendar

您好我创建了一个包含日历的片段

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/calendar"
        android:layout_gravity="center" />
    </LinearLayout>
</FrameLayout>

通过点击特定日期,我添加了一个操作,用于打开包含当天事件的新片段。 Theese事件应该存储在数据库中吗?如何为包含此类事件的日期着色 image

public class CalendarFragment extends Fragment {
    CalendarView calendar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_rendezvous, container, false);
        calendar = (CalendarView) rootview.findViewById(R.id.calendar);
        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override

            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                Intent intent = new Intent(getActivity(), Events.class);
                startActivity(intent);
            }
        });
        return rootview;
    }
}

提前致谢

3 个答案:

答案 0 :(得分:1)

尝试这样来装饰你的日期

public class OneDayDecorator implements DayViewDecorator {

    private CalendarDay date;

    public OneDayDecorator() {
        date = CalendarDay.today();
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return date != null && day.equals(date);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new ForegroundColorSpan(Color.RED));
    }

    /**
     * We're changing the internals, so make sure to call {@linkplain MaterialCalendarView#invalidateDecorators()}
     */
    public void setDate(Date date) {
        this.date = CalendarDay.from(date);
    }
}

然后申请装饰

mCalendarView.addDecorator(mSelectedDayDecorator);

答案 1 :(得分:0)

使用ExtendedCalendarView,易于处理  事件form here, you can got

答案 2 :(得分:0)

我使用了以下方式

    calendar =Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
selectedDay =CalendarDay.from(calendar);

calendarMonth.setSelectedDate(selectedDay);
calendarMonth.addDecorator(new

SelectDayDecorator(selectedDay));

calendarView.setOnDateChangedListener(new

OnDateSelectedListener() {
    @Override
    public void onDateSelected (@NonNull MaterialCalendarView widget, @NonNull CalendarDay date,
    boolean selected)
    {
        if (selected) {

            calendarView.addDecorator(new UnselectDayDecorator(selectedDay));
            selectedDay = date;
            calendarView.addDecorator(new SelectDayDecorator(date));


        }


    }
});


public class SelectDayDecorator implements DayViewDecorator {

    private CalendarDay date;

    public SelectDayDecorator(CalendarDay day) {
        date = day;
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return date != null && day.equals(date);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new ForegroundColorSpan(Color.parseColor("#444453")));
    }

    public void setDate(Date date) {
        this.date = CalendarDay.from(date);
    }
}


public class UnselectDayDecorator implements DayViewDecorator {

    private CalendarDay date;

    public UnselectDayDecorator(CalendarDay day) {
        date = day;
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return date != null && day.equals(date);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new ForegroundColorSpan(Color.WHITE));
    }

    public void setDate(Date date) {
        this.date = CalendarDay.from(date);
    }
}