我要更改 DatePicker 对话框的分隔线颜色。
我创建了样式:
<style name="dialog_custom" parent="@android:style/Widget.DatePicker">
<item name="android:divider">@drawable/dialog_divider</item>
</style>
并像这样创建可绘制对象
结果是这样的
分隔线没有改变颜色,对话框采用内容大小..
这是我在 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();
}
}
}
您可以使用主题来执行此操作。检查此问题的已接受答案。我认为这会对你有所帮助。
更新
展开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"