2015-09-11

[Android] 如何改變DatePicker的Divider的顏色 (How to change the divider color of the DatePicker)


這是一個可以自己從xml指定Divider的顏色的DatePicker

可以把像下圖這樣的Spinner DatePicker的Divider從藍色換成別的自己指定的顏色
We can change the divider color of DatePicker from blue to any other color you want.



首先要先在resource的drawable當中加入一個shape
把"THE_COLOR_YOU_WANT"換成你想要的顏色



Add a shape in your resource.
Replace the "THE_COLOR_YOU_WANT" to what you want.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/THE_COLOR_YOU_WANT"/>
</shape>


接下來加入DatePickerWithAppColorDivider這個Class

這個class extends原本的DatePicker
但是把divider換成剛剛在resource加入的shape了


import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.DatePicker;
import android.widget.NumberPicker;

import com.htc.babyface.R;

import java.lang.reflect.Field;

public class DatePickerWithAppColorDivider extends DatePicker {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public DatePickerWithAppColorDivider(Context context, AttributeSet attrs, int defStyleAttr, int
        defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        changeDividerColor();
    }

    public DatePickerWithAppColorDivider(Context context) {
        super(context);
        changeDividerColor();
    }

    public DatePickerWithAppColorDivider(Context context, AttributeSet attrs) {
        super(context, attrs);
        changeDividerColor();
    }

    public DatePickerWithAppColorDivider(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        changeDividerColor();
    }

    private void changeDividerColor() {
        try {
            Class internalRID = Class.forName("com.android.internal.R$id");
            Field month = internalRID.getField("month");
            Field day = internalRID.getField("day");
            Field year = internalRID.getField("year");
            NumberPicker npMonth = (NumberPicker) findViewById(month.getInt(null));
            NumberPicker npDay = (NumberPicker) findViewById(day.getInt(null));
            NumberPicker npYear = (NumberPicker) findViewById(year.getInt(null));
            setDividerColor(npMonth);
            setDividerColor(npDay);
            setDividerColor(npYear);
        } catch (Exception ignored) {
        }
    }

    private void setDividerColor(NumberPicker numberPicker) {
        Field[] numberPickerFields = NumberPicker.class.getDeclaredFields();
        for (Field field : numberPickerFields) {
            if (field.getName().equals("mSelectionDivider")) {
                field.setAccessible(true);
                try {
                    field.set(numberPicker, ContextCompat.getDrawable(getContext(),
                        R.drawable.spinner_date_picker_divider));
                } catch (Exception ignored) {
                }
                break;
            }
        }
    }

}




參考以下兩個網址

Android: how to change the color of the datepicker divider?
The way in this link to change divider color maybe out of date.
The "mSpinner" is no longer under DatePicker.
So we can't get mSpinner from DatePicker's field.