How to change TimePicker lines color in Appcompat theme?

不羁岁月 提交于 2019-12-06 08:25:55

问题


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

I use TimePickerDialog with ContextThemeWrapper.

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

and style

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

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

Thanks in advance


回答1:


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

You can change color apptheme_color only.

Thanks to mohammad rababah




回答2:


Expand res folder in your application and expand values folder. Then create themes.xml file on values folder. then replace all code in themes.xml file with below code.

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

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

</style>

Then open your AndroidManifest.xml file. and find android:theme and replcae with android:theme="@style/MYTheme"



来源:https://stackoverflow.com/questions/23040600/how-to-change-timepicker-lines-color-in-appcompat-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!