更改分隔符颜色Android DatePicker对话框

时间:2013-02-13 08:16:41

标签: android colors datepicker divider

我想改变DatePicker对话框的分隔颜色。

我创建了样式:

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

创建像this

这样的drawable

结果是this

分隔符不会改变颜色,对话框会占用内容大小..

2 个答案:

答案 0 :(得分:1)

这是我在NumberPickers,TimePickers,DatePickers和TimePickerDialog中更改分隔符颜色的解决方案。对于DatePickerDialog,您可以调用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();
    }
}

}

答案 1 :(得分:-1)

您可以使用主题执行此操作。查看此question上已接受的答案。我认为这对你有帮助。

<强>更新

展开应用程序中的res文件夹,然后展开values文件夹。然后在themes.xml文件夹上创建values文件。然后用以下代码替换themes.xml文件中的所有代码。

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

然后打开您的AndroidManifest.xml文件。并找到android:theme并与android:theme="@style/MYTheme"

重复