You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Applandeo Material-Calendar-View库的日历月份显示语言?

解决Material-Calendar-View自定义月份语言的方法

我之前也踩过这个坑!这个库的月份显示语言并不是直接跟随系统设置的,得通过代码手动配置自定义的日期格式化逻辑,下面是具体的解决步骤:

1. 定义自定义月份/星期文本数组

在你的项目资源目录中创建或修改字符串数组,比如在res/values/strings.xml中添加:

<string-array name="custom_months">
    <item>一月</item>
    <item>二月</item>
    <item>三月</item>
    <item>四月</item>
    <item>五月</item>
    <item>六月</item>
    <item>七月</item>
    <item>八月</item>
    <item>九月</item>
    <item>十月</item>
    <item>十一月</item>
    <item>十二月</item>
</string-array>
<string-array name="custom_week_days">
    <item>日</item>
    <item>一</item>
    <item>二</item>
    <item>三</item>
    <item>四</item>
    <item>五</item>
    <item>六</item>
</string-array>

如果需要多语言支持,就在对应语言的资源目录(比如res/values-en/strings.xml)中配置对应的英文数组即可。

2. 给CalendarView设置自定义DateFormatter

通过代码实现DateFormatter接口,替换默认的月份和星期显示逻辑:

Kotlin示例

val calendarView = findViewById<MaterialCalendarView>(R.id.your_calendar_id)
val months = resources.getStringArray(R.array.custom_months)
val weekDays = resources.getStringArray(R.array.custom_week_days)

calendarView.dateFormatter = object : DateFormatter {
    override fun formatMonth(month: Int): String {
        // month参数从0(一月)到11(十二月),直接对应数组索引
        return months[month]
    }

    override fun formatDayOfWeek(dayOfWeek: Int): String {
        // dayOfWeek从1(周日)到7(周六),减1匹配数组索引
        return weekDays[dayOfWeek - 1]
    }
}

Java示例

MaterialCalendarView calendarView = findViewById(R.id.your_calendar_id);
String[] months = getResources().getStringArray(R.array.custom_months);
String[] weekDays = getResources().getStringArray(R.array.custom_week_days);

calendarView.setDateFormatter(new DateFormatter() {
    @Override
    public String formatMonth(int month) {
        return months[month];
    }

    @Override
    public String formatDayOfWeek(int dayOfWeek) {
        return weekDays[dayOfWeek - 1];
    }
});

3. 排查系统语言不生效的问题

如果修改系统语言后日历没跟着变化,检查下你的AndroidManifest.xml中是否给Activity设置了android:configChanges="locale",这个配置会阻止系统自动刷新语言。如果有,建议移除该配置,或者在onConfigurationChanged方法中重新设置一次DateFormatter来刷新显示。

内容的提问来源于stack exchange,提问作者Vyslon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 13:58:14