CalendarView是一个日历控件,用它可以快速实现日历布局,而且布局可以左右滑动切换年月,非常方便实用。
我们可以简单的更改它的背景颜色,但是修改不了它的 字体大小和颜色:
要想修改,我们得在values的themes中设置 自定义样式使用:
然后就有如下效果了:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DateFragment">
<CalendarView
android:id="@+id/calendarView"
android:layout_width="450dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/Widget.CalendarView.Custom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
自定义样式:
<style name="TextAppearance.CalendarDayHeader" parent="TextAppearance.AppCompat.Medium">
<item name="android:textSize">18px</item> <!-- 设置字体大小 -->
<item name="android:textColor">@color/white</item> <!-- 设置字体颜色 -->
</style>
<style name="Widget.CalendarView.Custom" parent="Theme.AppCompat">
<!--周字体颜色-->
<item name="android:weekDayTextAppearance">@style/TextAppearance.CalendarDayHeader</item>
<!--日字体颜色-->
<item name="android:dateTextAppearance">@color/white</item>
<!--选中后背景-->
<item name="colorAccent">@color/black</item>
<item name="colorPrimary">@color/white</item>
<!--选中后字体颜色-->
<item name="android:textColorPrimaryInverse">@color/white</item>
<!--标题颜色-->
<item name="android:textColorPrimary">@color/white</item>
</style>