问题
I have a list of dates that I want to color in red on calendarview . how can I do ?
my activity..
public class Calendario extends Activity {
RelativeLayout rl;
final Calendar calendar = Calendar.getInstance();
CalendarView cal;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
rl = (RelativeLayout) findViewById(R.id.rl);
cal = new CalendarView(Calendario.this);
rl.addView(cal);
cal.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
// TODO Auto-generated method stub
}
});
}
}
What code i've to add in order to color a date ?
回答1:
Your best bet might be to create your own CalendarView class i.e. CalendarViewCustom based on the source code found here.
You could then add an extra method to the LegacyCalendarViewDelegate class similar to the setFocusedMonthDateColor() method to iterate through the weeks and set a date and colour in the WeekView class (might be worth storing these key/value date/colours as an Map collection instance variable in the WeekView class). e.g.
public void setMonthDateColor(Date date, int color) {
final int childCount = mListView.getChildCount();
for (int i = 0; i < childCount; i++) {
WeekView weekView = (WeekView) mListView.getChildAt(i);
if (weekView.isDateInWeek(date)) {
//this method adds the date and colour to a
//Map collection in weekView Object
weekView.setDateColour(date, color);
}
}
}
The above method then needs to be exposed by adding another method to the parent class CalendarViewCustom (similar to its existing methods) which can then be called on an instance of the class i.e.
public int setMonthDateColor(Date date, int color) {
return mDelegate.getMonthDateColor(date, color);
}
All you need to do then is draw the listed colours on the canvas in the WeekView class method called drawWeekNumbersAndDates() for the specified dates using the existing for loop ( for (; i < nDays; i++) ) and iterate over the Map and change the paint colour for the date text i.e. mMonthNumDrawPaint.setColor().
Hope this points you in the right direction.
回答2:
Try this. Hope this helps
android:weekDayTextAppearance="@style/CalendarWeekDateText"
<style name="CalendarWeekDatText" parent="TextAppearance.AppCompat.Button">
<item name="android:textColor">#000000</item>
</style>
回答3:
android:theme="@style/testTheme"
Use this theme or a custom theme which has parent as this theme.
To make it white
To Choose other than white change the color use the following
android:textColorPrimary="@color/yourColor"
for other text colors use the following
android:weekDayTextAppearance="@style/weekDayTextAppearance"
android:dateTextAppearance="@style/appTextAppearance"
android:unfocusedMonthDateColor="@color/colorLoginBtn"
android:selectedWeekBackgroundColor="@color/colorLoginBtn"
android:weekSeparatorLineColor="@color/colorLoginBtn"
android:focusedMonthDateColor="@color/colorLoginBtn"
android:weekNumberColor="@color/colorLoginBtn"
来源:https://stackoverflow.com/questions/26997042/android-calendar-view-date-color