android - 如何更改 Lollipop 中的 DatePicker 日历 View 背景颜色?

标签 android android-5.0-lollipop android-custom-view

我花了很大力气才改变了 Lollipop 的 DatePicker 的默认背景颜色。我不能简单地使用 Styleable attrs 来更改默认样式。正如另一个post中提到的,我只能使用反射来找到 View ,然后对其进行更改。

例如

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 的上半部分(见下文)。

DatePicker的上半部分 enter image description here

但是,对于日历 View 的下半部分(见下文),我无法使用相同的方法进行更改,因为我找不到该 View (我试图通过 id R.id.calendar_view 找到该 View ,但它不管用。)。

DatePicker 的下半部分 enter image description here

准确地说,我想更改圈出日期的背景颜色和当前日期的文本颜色(在本例中为2014年3月7日)

有什么提示吗?非常感谢。

更新:

翻阅文档后,我发现下半部分的日历实际上是一个 SimpleMonthView.class,并且圆圈的背景颜色和当前日期的 textColor 都由参数 (int) mSelectedDayColor 控制。

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

我无法使用以前的方法,因为日历是在 onDraw 方法中以编程方式创建的,而不是通过膨胀布局文件来创建的。

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

谢谢..

更新: 在研究了 alanv 的解决方案后,我尝试了以下方法: 由于我正在开发 Lollipop 的 DatePicker,因此我将以下内容放入 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。

在我的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"/>

但对 Lollipop 的 DatePicker 没有影响。

最佳答案

任何使用反射的代码都可能在未来的操作系统更新时中断。您永远不应该使用反射来访问私有(private) API 或值。

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

res/values/styles.xml:

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

res/layout/my_layout.xml:

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

关于android - 如何更改 Lollipop 中的 DatePicker 日历 View 背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29170874/

相关文章:

android - SDK 管理器不会启动 Windows 7 64 位?

android - 如何检测 Android 上的语言是否为英语(所有变体)?

Android Camera2 getPreviewFrame

android - 获取 NullPointerException : Attempt to read from field 'int android.view.View.mViewFlags' - Android animation

Android Lollipop 从 native 代码c++写入sdcard

android - 更新 ADT 23 后,SDK 路径从首选项中丢失

android-custom-view - paypal android sdk的自定义支付屏幕

android - 在 ListView 中放置一个自定义 View

Android 复合 View : cannot make child view match parent

android - 更改文本 Controller 上的所有颜色