为禁用日期在 CalendarView 上设置不同的颜色。

huangapple go评论62阅读模式
英文:

Different color for disabled dates CalendarView

问题

我创建了一个自定义的 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:

为禁用日期在 CalendarView 上设置不同的颜色。

I want all of the past days to be in a different color.

My XML is:

&lt;CalendarView
    android:id=&quot;@+id/cv_Calendar&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;300dp&quot;
    android:layout_marginStart=&quot;16dp&quot;
    android:layout_marginEnd=&quot;16dp&quot;
    android:background=&quot;@drawable/cv_background&quot;
    android:dateTextAppearance=&quot;@style/CalenderViewDateCustomText&quot;
    android:theme=&quot;@style/CalenderViewCustom&quot;
    android:weekDayTextAppearance=&quot;@style/CalenderViewWeekCustomText&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/tv_With&quot; /&gt;

And my styles are:

&lt;style name=&quot;CalenderViewCustom&quot; parent=&quot;Theme.AppCompat&quot;&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;@color/colorLightGray&lt;/item&gt;
    &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorLightPurple&lt;/item&gt;
    &lt;item name=&quot;android:textColorPrimary&quot;&gt;@color/colorGrayText&lt;/item&gt;
&lt;/style&gt;

&lt;style name=&quot;CalenderViewDateCustomText&quot; parent=&quot;android:TextAppearance.Holo.Small&quot;&gt;
    &lt;item name=&quot;android:textColor&quot;&gt;@color/colorLightPurple&lt;/item&gt;
    &lt;item name=&quot;android:weekNumberColor&quot;&gt;@color/colorLightPurple&lt;/item&gt;
&lt;/style&gt;

&lt;style name=&quot;CalenderViewWeekCustomText&quot; parent=&quot;android:TextAppearance.DeviceDefault.Small&quot;&gt;
    &lt;item name=&quot;android:textColor&quot;&gt;@color/colorLightPurple&lt;/item&gt;
&lt;/style&gt;

I set the minimum date programmatically:

calendarView = findViewById( R.id.cv_Calendar );
calendarView.setMinDate( System.currentTimeMillis() );
calendarView.setOnDateChangeListener( (arg0, year, month, date) -&gt; {
    GregorianCalendar cal = new GregorianCalendar( year, month, date );
    dateInMillis = cal.getTimeInMillis();
} );

Any idea how to change the color?

Thank you

答案1

得分: 4

以下为翻译好的部分:

我不确定您是否仍然需要对此问题的答案,但以防其他人遇到这个问题,以下是如何操作:

基本上,您需要更改您正在使用的 textColor 属性,使其引用一个类似于这样的选择器:

selectorDateTextColor.xml
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
        
    &lt;!-- 禁用日期使用的颜色(即范围超出您使用 minDate 和 maxDate 设置的范围的日期) --&gt;
    &lt;item android:color=&quot;@color/gray&quot; android:state_enabled=&quot;false&quot; /&gt;
    
    &lt;!-- 其他所有日期使用的颜色 --&gt;
    &lt;item android:color=&quot;@color/colorLightPurple&quot; /&gt;
&lt;/selector&gt;

您的样式应为:

&lt;style name=&quot;CalenderViewDateCustomText&quot; parent=&quot;android:TextAppearance.Holo.Small&quot;&gt;
    &lt;item name=&quot;android:textColor&quot;&gt;@color/selectorDateTextColor&lt;/item&gt;
    &lt;item name=&quot;android:weekNumberColor&quot;&gt;@color/colorLightPurple&lt;/item&gt;
&lt;/style&gt;

您还可以添加更多状态,比如 android:state_activated=&quot;true&quot;,以更改用户选择的日期的文本颜色。

如果您想要更改所选日期周围圆圈的颜色,您应该在您的 CalendarView 使用的主题中添加 &lt;item name=&quot;colorControlActivated&quot;&gt;@color/blue&lt;/item&gt;

英文:

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
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
        
    &lt;!-- The color used by the disabled dates (i.e. everything outside the range you set with minDate and maxDate) --&gt;
    &lt;item android:color=&quot;@color/gray&quot; android:state_enabled=&quot;false&quot; /&gt;
    
    &lt;!-- The color used by everything else --&gt;
    &lt;item android:color=&quot;@color/colorLightPurple&quot; /&gt;
&lt;/selector&gt;

And your style would be:

&lt;style name=&quot;CalenderViewDateCustomText&quot; parent=&quot;android:TextAppearance.Holo.Small&quot;&gt;
    &lt;item name=&quot;android:textColor&quot;&gt;@color/selectorDateTextColor&lt;/item&gt;
    &lt;item name=&quot;android:weekNumberColor&quot;&gt;@color/colorLightPurple&lt;/item&gt;
&lt;/style&gt;

You can also add more states such as android:state_activated=&quot;true&quot; 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 &lt;item name=&quot;colorControlActivated&quot;&gt;@color/blue&lt;/item&gt; to the theme used by your CalendarView.

huangapple
  • 本文由 发表于 2020年8月17日 00:01:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63439022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定