如何在Appcompat主题中更改TimePicker线条颜色?

时间:2014-04-13 08:34:35

标签: android styling timepicker android-appcompat

如何在Appcompat主题中选择的数字之间更改TimePicker线颜色?线是蓝色的,但我需要橙色线。

我将TimePickerDialog与ContextThemeWrapper一起使用。

   TimePickerDialog timePicker = new TimePickerDialog(
        new ContextThemeWrapper(getActivity(), R.style.timePicker), 
    this, hour, minute, DateFormat.is24HourFormat(getActivity()));

和风格

<style name="timePicker">
    <item name="android:divider">@drawable/cab_background_top_play</item>
</style>

但该行可能还有其他ID。我不确定只使用drawable或color。

提前致谢

2 个答案:

答案 0 :(得分:7)

我用自己的Timepicker类解决了这个问题,它扩展了android Timepicker类。我的帖子stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider/20291416#20291416启发了mohammad rababah,在我的提问下面发表评论{/ 3}}。

这是样式的timepicker类:​​

public class StyledTimePicker extends TimePicker {

        public StyledTimePicker(Context context, AttributeSet attrs) {
            super(context, attrs);

            Class<?> internalRID = null;
            try {
                internalRID = Class.forName("com.android.internal.R$id");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            Field month = null;
            try {
                month = internalRID.getField("hour");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            NumberPicker npMonth = null;
            try {
                npMonth = (NumberPicker) findViewById(month.getInt(null));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            Field day = null;
            try {
                day = internalRID.getField("minute");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            NumberPicker npDay = null;
            try {
                npDay = (NumberPicker) findViewById(day.getInt(null));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            Field year = null;
            try {
                year = internalRID.getField("amPm");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            NumberPicker npYear = null;
            try {
                npYear = (NumberPicker) findViewById(year.getInt(null));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            Class<?> numberPickerClass = null;
            try {
                numberPickerClass = Class.forName("android.widget.NumberPicker");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            Field selectionDivider = null;
            try {
                selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            try {
                selectionDivider.setAccessible(true);
                selectionDivider.set(npMonth, getResources().getDrawable(
                        R.color.apptheme_color));
                selectionDivider.set(npDay, getResources().getDrawable(
                        R.color.apptheme_color));
                selectionDivider.set(npYear, getResources().getDrawable(
                        R.color.apptheme_color));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
}

您只能更改颜色apptheme_color。

感谢mohammad rababah

答案 1 :(得分:0)

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

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

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

</style>

然后打开AndroidManifest.xml文件。并找到android:theme和replcae与android:theme =“@ style / MYTheme”