2

我要更改 DatePicker 对话框的分隔线颜色。

我创建了样式:

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

并像这样创建可绘制对象

结果是这样的

分隔线没有改变颜色,对话框采用内容大小..

4

2 回答 2

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

}

于 2014-12-17T12:14:15.493 回答
-1

您可以使用主题来执行此操作。检查此问题的已接受答案。我认为这会对你有所帮助。

更新

展开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"

于 2013-02-13T08:25:24.547 回答