Change Divider Color Android DatePicker Dialog

瘦欲@ 提交于 2019-12-24 00:52:26

问题


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..


回答1:


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"




回答2:


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();
    }
}

}



来源:https://stackoverflow.com/questions/14849147/change-divider-color-android-datepicker-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!