在日历视图中更改当天的文本颜色

时间:2019-10-24 18:19:24

标签: android android-view android-xml android-styles calendarview

我正在尝试在CalendarView中使用以下样式:

enter image description here

我设法做到的是:

enter image description here

问题是即时消息所在的日期的颜色是白色,标记也是白色。

如何将特定日期的颜色更改为黑色?

我的xml是:

<CalendarView
    android:id="@+id/cv_BorrowCalendar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cv_background"
    android:theme="@style/CalenderViewCustom"
    android:dateTextAppearance="@style/CalenderViewDateCustomText"
    android:weekDayTextAppearance="@style/CalenderViewWeekCustomText"
    app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
    app:layout_constraintTop_toBottomOf="@+id/rv_Borrowing" />

和样式:

<!-- Calendar -->
<style name="CalenderViewCustom" parent="Theme.AppCompat">
    <item name="colorAccent">@color/colorWhite</item>
    <item name="colorPrimary">@color/colorWhite</item>
</style>

<style name="CalenderViewDateCustomText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@color/colorWhite</item>
    <item name="android:weekNumberColor">@color/colorLightPurple</item>
</style>

<style name="CalenderViewWeekCustomText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@color/colorWhite</item>
</style>

此外,如果有一种方法可以将日期名称更改为sun而不是s或mon而不是m,那么很高兴知道=] 谢谢

3 个答案:

答案 0 :(得分:1)

选项1:

  

在CalendarView中设置样式

<CalendarView
    android:id="@+id/calendarView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:theme="@style/CalenderViewCustom"
    android:dateTextAppearance="@style/CalenderViewDateCustomText"
    android:weekDayTextAppearance="@style/CalenderViewWeekCustomText" />
  

Inside Style.xml

<style name="CalenderViewCustom" parent="Theme.AppCompat">
    <item name="colorAccent">@color/red</item>
    <item name="colorPrimary">@color/white</item>
</style>

<style name="CalenderViewDateCustomText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@color/white</item>
    <item name="android:weekNumberColor">@color/red</item>
</style>

<style name="CalenderViewWeekCustomText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@color/white</item>
</style>

选项2:

android:theme="@style/testTheme"

使用该主题或具有父主题的自定义主题。

使它变白

要选择除白色以外的其他颜色,请使用以下

android:textColorPrimary="@color/yourColor"

或其他文本颜色使用以下

<CalendarView
    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"/>

答案 1 :(得分:0)

尝试以下代码:

<CalendarView
    android:id="@+id/cv_BorrowCalendar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cv_background"
    android:theme="@style/CalenderViewCustom"
    android:weekDayTextAppearance="@style/CalenderViewWeekCustomText"/>

使用以上代码的输出为:

enter image description here 希望它对您有用

答案 2 :(得分:0)

设置CalendarView主题的样式:重要,您必须添加textColorPrimaryInverse是您想要的颜色。

<style name="CalenderViewCustom" parent="yourAppBase">
    <item name="colorControlActivated">@color/bg_app_base</item>
    <item name="android:textColorPrimaryInverse">@color/white</item>  // color of date selected.
</style>

然后在.xml上设置主题

<CalendarView
   android:id="@+id/calendarView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:theme="@style/CalenderViewCustom" />