Change Divider Color Android DatePicker Dialog

2019-07-11 13:52发布

I´m tying to change the divider color of the DatePicker Dialog.

I create the style:

<style name="dialog_custom" parent="@android:style/Widget.DatePicker">
        <item name="android:divider">@drawable/dialog_divider</item>
    </style>

And create the drawable like this

And the result is this

The divider no change color and the dialog take the content size..

2条回答
神经病院院长
2楼-- · 2019-07-11 14:27

This is my solution to change divider colors in NumberPickers, TimePickers, DatePickers and the TimePickerDialog. For DatePickerDialog you can call DatePickerDialog.getDatePicker()

public class NumberPickerStylingUtils {

private static final Drawable PICKER_DIVIDER_DRAWABLE = //Place your drawable here

private NumberPickerStylingUtils() {}

public static void applyStyling(TimePickerDialog timePickerDialog) {
    try {
        Field field = TimePickerDialog.class.getDeclaredField("mTimePicker");
        field.setAccessible(true);
        applyStyling((TimePicker) field.get(timePickerDialog));
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void applyStyling(TimePicker timePicker) {
    try {
        Field fields[] = TimePicker.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().equals(NumberPicker.class)) {
                field.setAccessible(true);
                applyStyling((NumberPicker) field.get(timePicker));
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void applyStyling(DatePicker datePicker) {
    try {
        Field fields[] = DatePicker.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().equals(NumberPicker.class)) {
                field.setAccessible(true);
                applyStyling((NumberPicker) field.get(datePicker));
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void applyStyling(NumberPicker numberPicker) {
    try {
        Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
        field.setAccessible(true);
        field.set(numberPicker, PICKER_DIVIDER_DRAWABLE));
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

}

查看更多
走好不送
3楼-- · 2019-07-11 14:32

You can do this using theme. Check accepted answer on this question. i think it will helpful to you.

UPDATES

Expand res folder in your application and expand values folder. Then create themes.xml file on values folder. then replace all code in themes.xml file with below code.

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="MYTheme" parent="@android:style/Theme">

       <item name="android:divider">@drawable/dialog_divider</item>

    </style>

</resources>

Then open your AndroidManifest.xml file. and find android:theme and replcae with android:theme="@style/MYTheme"

查看更多
登录 后发表回答