如何在Appcompat主题中更改TimePicker线条的颜色? [英] How to change TimePicker lines color in Appcompat theme?

查看:81
本文介绍了如何在Appcompat主题中更改TimePicker线条的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to change TimePicker lines color between choosed number in Appcompat theme? The lines are blue, but i need orange lines.

我将TimePickerDialog与ContextThemeWrapper一起使用.

I use TimePickerDialog with 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.

but the line have other id maybe. I'm not sure to use drawable or color only.

预先感谢

推荐答案

我用自己的Timepicker类解决了这个问题,该类扩展了Android Timepicker类.我的灵感来自帖子 stackoverflow.com/Questions/20148671/android-how-to-to-to-change-the-color-of-datepicker-divider/20291416#20291416 其中发布了

I solved this with own Timepicker class which extends android Timepicker class. I inspired by post stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider/20291416#20291416 which posted mohammad rababah in comment below my ask.

以下是样式化的时间选择器类:

Here is styled timepicker class:

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.

You can change color apptheme_color only.

感谢 mohammad rababah

这篇关于如何在Appcompat主题中更改TimePicker线条的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆