Kotlin CalendarView 更改月份和日期文本

问题描述 投票:0回答:1

我在我的项目中使用 CalendarView。但在我的语言中,月份和日期的名称是不同的。而且我已经想让我的应用程序与其他语言兼容。但我在日历视图中看不到任何关于“更改文本”的内容。实际上我正在尝试做一个事件应用程序。你知道它的经典之处,用户选择一个日期,并且选择的日期变成不同的颜色。我为此下载了一个库,但由于这个文本问题我删除了它。我什至尝试将项目下载为模块并更改其字符串值。我也做到了,但这次我不能用它来代替原始库。如果您知道更改其文本的方法或易于使用的库,请告诉我。

kotlin calendarview
1个回答
0
投票

这是一个很棒的图书馆用于日历视图kizitonwose/calendar 您也可以根据用户区域设置自由自定义日期标签。

您还应该使用 daysOfWeek 列表值来设置工作日标题,这样它与 CalendarView 上显示的相匹配。

要设置星期几标题,您可以使用月份标题显示每个月的标题并允许标题随月份滚动,或者您可以在日历上方的静态视图中显示标题。下面介绍了两种方法:

使用静态视图设置星期几:

第一步 在 res/layout/calendar_day_title_text.xml 中创建标题文本视图。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center" />

Create a container resource to use the text in res/layout/calendar_day_titles_container.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="7"
    android:orientation="horizontal">
    
    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

</LinearLayout>

在与 CalendarView 相同的布局中添加标题容器:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/titlesContainer"
        layout="@layout/calendar_day_titles_container" />

    <com.kizitonwose.calendar.view.CalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cv_dayViewResource="@layout/calendar_day_layout" />

</LinearLayout>

第二步 现在您可以使用之前讨论的 daysOfWeek 列表设置标题:

val titlesContainer = findViewById<ViewGroup>(R.id.titlesContainer)
titlesContainer.children
    .map { it as TextView }
    .forEachIndexed { index, textView ->
        val dayOfWeek = daysOfWeek[index]
        val title = dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.getDefault())
        textView.text = title
    }

更多参考请关注自定义工作日标签

© www.soinside.com 2019 - 2024. All rights reserved.