如何在棒棒糖中更改DatePicker的CalendarView的背景颜色? [英] How to change DatePicker's CalendarView's background color in Lollipop?

查看:144
本文介绍了如何在棒棒糖中更改DatePicker的CalendarView的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常努力地更改Lollipop的DatePicker的默认背景颜色.我不能简单地使用Styleable attrs来更改默认样式.正如另一篇帖子所述,我只能使用反射来查找视图,然后对其进行更改.

I worked so hard to change the default background color of Lollipop's DatePicker. I cannot simply use Styleable attrs to change the default style. And as mentioned in another post, I can only use reflection to find the view, and then make changes on it.

例如

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    int monthDayYearLayoutId = Resources.getSystem().getIdentifier("date_picker_month_day_year_layout", "id", "android");
    if (monthDayYearLayoutId != 0) {
        View monthDayYearLayout = datePicker.findViewById(monthDayYearLayoutId);
        if (monthDayYearLayout != null) {
            monthDayYearLayout.setBackgroundColor(getResources().getColor(R.color.colorful));
        } 
    } 
} 

但是,我只能访问DatePicker的上半部分(见下文).

However, I am only able to access the upper half of the DatePicker (see below).

DatePicker的上半部分

DatePicker's upper half

但是,对于下半部分,即CalendarView(请参见下文),我无法使用相同的方法进行更改,因为找不到视图(我尝试通过ID R.id.calendar_view查找视图,但是不管用.).

However,for the lower half which is a CalendarView (see below), I cannot change using the same approach, as I cannot find the view (I tried to find the view by the id R.id.calendar_view, yet it is not working.).

DatePicker的下半部分

DatePicker's lower half

确切地说,我想更改带圆圈的日期的背景色和当前日期的textColor(在这种情况下,该日期为2014年3月7日)

To be precise, I would like to change the background color of the circled date, and the textColor for the present date (in this case, it is 7th March, 2014)

有任何提示吗?非常感谢.

Any hints? Thanks a lot.

更新:

浏览文档后,我发现下半部分的日历实际上是SimpleMonthView.class,并且圆圈的背景颜色和当前的textColor均由参数mSelectedDayColor决定.

After looking through the docs, I found that the lower half's calendar is in fact a SimpleMonthView.class, and the background color of the circle and the textColor for the present day are both governed by the param (int) mSelectedDayColor.

    mSelectedDayColor = colors.getColorForState(ENABLED_SELECTED_STATE_SET,
            res.getColor(R.color.holo_blue_light));

我不能使用以前的方法,因为日历是通过onDraw方法以编程方式创建的,而不是通过填充布局文件来创建的.

I cannot use the previous method, since the calendar is created in the onDraw method programmatically, but not by inflating a layout file.

所以问题归结为-我如何更改mSelectedDayColor的资源值?

So the problem boils down to - how could I change the resource value for mSelectedDayColor?

谢谢..

更新:在研究alanv的解决方案之后,我尝试了以下方法:由于我正在研究Lollipop的DatePicker,因此将以下内容放入v21-styles.xml:

Updates: After working on alanv's solution, I tried this: Since I am working on Lollipop's DatePicker, I put the following in v21-styles.xml:

<style name="MyCalendarView" parent="@android:style/Widget.CalendarView">
    <item name="android:showWeekNumber">true</item>
    <item name="android:minDate">01/01/2016</item>
    <item name="android:maxDate">12/31/2100</item>
    <item name="android:shownWeekCount">6</item>
    <item name="android:selectedWeekBackgroundColor">#330099FF</item>
    <item name="android:focusedMonthDateColor">#FFFFFFFF</item>
    <item name="android:unfocusedMonthDateColor">#66FFFFFF</item>
    <item name="android:weekNumberColor">#33FFFFFF</item>
    <item name="android:weekSeparatorLineColor">#19FFFFFF</item>
</style>

然后我更改了一些默认值,例如.android:minDate.

And I changed some of the default values, eg. android:minDate.

在我的activity_main.xml中,

And in my activity_main.xml,

<DatePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/date_picker"
    android:layout_centerHorizontal="true"
    android:theme="@style/MyCalendarView"/>

但是对棒棒糖的DatePicker没有影响.

But there is no effect on DatePicker for Lollipop.

推荐答案

任何使用反射的代码都可能在将来的OS更新中中断.绝对不要使用反射来访问私有API或值.

Any code using reflection may break on future OS updates. You should never use reflection to access private APIs or values.

最简单的方法是创建一个重新定义 android:colorAccent 的覆盖主题,并将其应用于您的 DatePicker .

The easiest way would be to create an overlay theme that redefines android:colorAccent and apply that to your DatePicker.

res/values/styles.xml:

res/values/styles.xml:

<style name="MyThemeOverlay">
    <item name="android:colorAccent">@color/my_accent</item>
</style>

res/layout/my_layout.xml:

res/layout/my_layout.xml:

<DatePicker
    ...
    android:theme="@style/MyThemeOverlay" />

这篇关于如何在棒棒糖中更改DatePicker的CalendarView的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆