英文:
Different color for disabled dates CalendarView
问题
我创建了一个自定义的 CalendarView,外观如下所示:
我的目标是将过去的所有日期显示为不同的颜色。
我的 XML 如下:
<CalendarView
android:id="@+id/cv_Calendar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/cv_background"
android:dateTextAppearance="@style/CalenderViewDateCustomText"
android:theme="@style/CalenderViewCustom"
android:weekDayTextAppearance="@style/CalenderViewWeekCustomText"
app:layout_constraintTop_toBottomOf="@+id/tv_With" />
我的样式如下:
<style name="CalenderViewCustom" parent="Theme.AppCompat">
<item name="colorAccent">@color/colorLightGray</item>
<item name="colorPrimary">@color/colorLightPurple</item>
<item name="android:textColorPrimary">@color/colorGrayText</item>
</style>
<style name="CalenderViewDateCustomText" parent="android:TextAppearance.Holo.Small">
<item name="android:textColor">@color/colorLightPurple</item>
<item name="android:weekNumberColor">@color/colorLightPurple</item>
</style>
<style name="CalenderViewWeekCustomText" parent="android:TextAppearance.DeviceDefault.Small">
<item name="android:textColor">@color/colorLightPurple</item>
</style>
我通过编程方式设置了最小日期:
calendarView = findViewById(R.id.cv_Calendar);
calendarView.setMinDate(System.currentTimeMillis());
calendarView.setOnDateChangeListener((arg0, year, month, date) -> {
GregorianCalendar cal = new GregorianCalendar(year, month, date);
dateInMillis = cal.getTimeInMillis();
});
有什么办法可以改变颜色吗?
谢谢
英文:
I created a custom CalendarView that looks as follows:
I want all of the past days to be in a different color.
My XML is:
<CalendarView
android:id="@+id/cv_Calendar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/cv_background"
android:dateTextAppearance="@style/CalenderViewDateCustomText"
android:theme="@style/CalenderViewCustom"
android:weekDayTextAppearance="@style/CalenderViewWeekCustomText"
app:layout_constraintTop_toBottomOf="@+id/tv_With" />
And my styles are:
<style name="CalenderViewCustom" parent="Theme.AppCompat">
<item name="colorAccent">@color/colorLightGray</item>
<item name="colorPrimary">@color/colorLightPurple</item>
<item name="android:textColorPrimary">@color/colorGrayText</item>
</style>
<style name="CalenderViewDateCustomText" parent="android:TextAppearance.Holo.Small">
<item name="android:textColor">@color/colorLightPurple</item>
<item name="android:weekNumberColor">@color/colorLightPurple</item>
</style>
<style name="CalenderViewWeekCustomText" parent="android:TextAppearance.DeviceDefault.Small">
<item name="android:textColor">@color/colorLightPurple</item>
</style>
I set the minimum date programmatically:
calendarView = findViewById( R.id.cv_Calendar );
calendarView.setMinDate( System.currentTimeMillis() );
calendarView.setOnDateChangeListener( (arg0, year, month, date) -> {
GregorianCalendar cal = new GregorianCalendar( year, month, date );
dateInMillis = cal.getTimeInMillis();
} );
Any idea how to change the color?
Thank you
答案1
得分: 4
以下为翻译好的部分:
我不确定您是否仍然需要对此问题的答案,但以防其他人遇到这个问题,以下是如何操作:
基本上,您需要更改您正在使用的 textColor
属性,使其引用一个类似于这样的选择器:
selectorDateTextColor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 禁用日期使用的颜色(即范围超出您使用 minDate 和 maxDate 设置的范围的日期) -->
<item android:color="@color/gray" android:state_enabled="false" />
<!-- 其他所有日期使用的颜色 -->
<item android:color="@color/colorLightPurple" />
</selector>
您的样式应为:
<style name="CalenderViewDateCustomText" parent="android:TextAppearance.Holo.Small">
<item name="android:textColor">@color/selectorDateTextColor</item>
<item name="android:weekNumberColor">@color/colorLightPurple</item>
</style>
您还可以添加更多状态,比如 android:state_activated="true"
,以更改用户选择的日期的文本颜色。
如果您想要更改所选日期周围圆圈的颜色,您应该在您的 CalendarView
使用的主题中添加 <item name="colorControlActivated">@color/blue</item>
。
英文:
I'm not sure if you still need an answer for this, but in case anyone else comes across this question, here's how you do it:
Basically you need to change the textColor
attribute you're using to reference a selector that should look like this:
selectorDateTextColor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The color used by the disabled dates (i.e. everything outside the range you set with minDate and maxDate) -->
<item android:color="@color/gray" android:state_enabled="false" />
<!-- The color used by everything else -->
<item android:color="@color/colorLightPurple" />
</selector>
And your style would be:
<style name="CalenderViewDateCustomText" parent="android:TextAppearance.Holo.Small">
<item name="android:textColor">@color/selectorDateTextColor</item>
<item name="android:weekNumberColor">@color/colorLightPurple</item>
</style>
You can also add more states such as android:state_activated="true"
to change the text color of the date selected by the user.
And if you want to change the color of the circle around the selected date you should add <item name="colorControlActivated">@color/blue</item>
to the theme used by your CalendarView
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论