设置日历视图月份名称的文本颜色视图、文本、日历、颜色

2023-09-06 11:37:18 作者:華燈初上

在Android中,我使用的是默认CalendarView。我已经将背景设置为浅灰色的颜色。但我可以改变日历视图月视图的颜色?

In android , i am using an default CalendarView. I have set the background to light gray color. But can i change the color of month view of calendar view?

推荐答案

正如你所发现,TextView的是私有的,而且似乎没有任何方法来访问它。

As you've found out, the TextView is private, and there does not seem to be any methods for accessing it.

虽然我不会推荐它,你可以使用的java.lang.reflect 封装实现这一点:

Although I wouldn't recommend it, you can accomplish this using the java.lang.reflect package:

    try
    {
        CalendarView cv = (CalendarView) this.findViewById(R.id.calendarView1);
        Class<?> cvClass = cv.getClass();
        Field field = cvClass.getDeclaredField("mMonthName");
        field.setAccessible(true);

        try
        {
            TextView tv = (TextView) field.get(cv);
            tv.setTextColor(Color.RED);
        } 
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
    catch (NoSuchFieldException e)
    {
        e.printStackTrace();
    }