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

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

问题描述

我辛辛苦苦改变棒棒堂的的DatePicker的默认背景色。我不能简单地用设置样式ATTRS更改默认的风格。和中提到的另一个<一href="http://stackoverflow.com/questions/26460682/custom-date-picker-dialog-in-android-lollipop">post,我只可以使用反射找到视图,然后进行就可以了变化。

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

要为precise,我想改变盘旋日期的背景颜色和文字颜色为present日期(在这种情况下,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)

任何提示?非常感谢。

更新

在翻翻文档,我发现,下半部分的日历实际上是一个SimpleMonthView.class,和圆的背景颜色和文字颜色为present一天都管辖的参数(INT) 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));

我不能使用previous方法,因为日历是在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的解决方案后,我尝试这样做: 由于我工作的棒棒堂的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>

和我改变了一些默认值,例如。机器人:为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.

推荐答案

使用反射可能会破坏未来的操作系统更新任何code。你不应该使用反射来访问私有API或值。

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

最简单的方法是创建一个覆盖的主题,重新定义了安卓colorAccent 并应用到你的的DatePicker

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

RES /价值/ styles.xml:

res/values/styles.xml:

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

RES /布局/ my_layout.xml:

res/layout/my_layout.xml:

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

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

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