Android分频器颜色DatePicker对话框

时间:2013-09-10 08:46:45

标签: android android-layout colors datepicker divider

我正在尝试在对话框中更改DatePicker的蓝色分隔线的颜色。这只是一个带有DatePicker和ButtonBar的普通DialogFragment。

有没有人知道要更改这些分隔符,或者如果没有用自定义的替换整个DatePicker,它是否可能?

Screenshot DatePicker

迷你咆哮

现在我看到太多的答案提示以下代码:

<style name="datePickerTheme" parent="@android:style/Widget.DeviceDefault.DatePicker">
    <item name="android:divider">**your @drawable/ or @color/ here**</item>
</style>

这根本行不通。你们在建议这段代码之前试过这个吗?它应该完美地工作,但它似乎不适用于DatePicker。

8 个答案:

答案 0 :(得分:22)

以下方法适用于我。这为所有字段设置分隔颜色(也适用于上午/下午)

 private void applyStyLing(TimePickerDialog timePickerDialog){
    Resources system = Resources.getSystem();
    int hourNumberPickerId = system.getIdentifier("hour", "id", "android");
    int minuteNumberPickerId = system.getIdentifier("minute", "id", "android");
    int ampmNumberPickerId = system.getIdentifier("amPm", "id", "android");

    NumberPicker hourNumberPicker = (NumberPicker) timePickerDialog.findViewById(hourNumberPickerId);
    NumberPicker minuteNumberPicker = (NumberPicker) timePickerDialog.findViewById(minuteNumberPickerId);
    NumberPicker ampmNumberPicker = (NumberPicker) timePickerDialog.findViewById(ampmNumberPickerId);

   setNumberPickerDividerColour(hourNumberPicker);
   setNumberPickerDividerColour(minuteNumberPicker);
   setNumberPickerDividerColour(ampmNumberPicker);
}

private void setNumberPickerDividerColour(NumberPicker number_picker){
    final int count = number_picker.getChildCount();

    for(int i = 0; i < count; i++){

        try{
            Field dividerField = number_picker.getClass().getDeclaredField("mSelectionDivider");
            dividerField.setAccessible(true);
                ColorDrawable colorDrawable = new ColorDrawable(mContext.getResources().getColor(R.color
                        .interactive_color));
            dividerField.set(number_picker,colorDrawable);

            number_picker.invalidate();
        }
        catch(NoSuchFieldException e){
            Log.w("setNumberPickerTxtClr", e);
        }
        catch(IllegalAccessException e){
            Log.w("setNumberPickerTxtClr", e);
        }
        catch(IllegalArgumentException e){
            Log.w("setNumberPickerTxtClr", e);
        }
    }
}

答案 1 :(得分:17)

我这样解决了这个问题:

我通过找到“DatePicker”将mSelectionDivider分隔符更改为反射。然后我遇到标题分隔符看起来很愚蠢的问题,所以我在包含3 textview的{​​{1}}上方添加了LinearLayout并使用了datepickers  删除原来的。

divider drawable的例子:对这个人的信任! :) http://ge.tt/8wK7TZ71/v/0?c

Result picture &lt; -My result

示例:

newFragment.setTitle("");

答案 2 :(得分:14)

我将此代码基于Ajit's answer,但我将其调整为122 122 122 121 1 0 0 -3 -3 而不是ceil() { ruby -ne 'puts $_.to_f.ceil'; }。此外,为了保持安全,我添加了DatePicker支票:

TimePicker

输出

colored dividers

答案 3 :(得分:6)

这解决了我的问题,将以下代码行添加到App主题样式。

<item name="colorControlNormal">@color/colorAccent</item>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/blue</item> 
</style>

答案 4 :(得分:4)

考虑到:

您不需要新的R.drawable.divider

你可以写:

 pf.set(number_picker, new ColorDrawable(getResources().getColor(R.color.red)));

答案 5 :(得分:2)

此版本已针对 DatePicker 进行了特定用途调整。虽然 Andrea Lazzarotto的版本运行良好,但它使用了一个不必要的循环,导致颜色变化的多个设备。除了轻微的代码改进,我的版本使用应用主题的主要颜色来匹配分频器颜色(请参阅How can I get the primary color from my app theme?)。使用Android 6.0和8.0测试:

private void colorizeDatePicker(final DatePicker datePicker) {
    final Resources system = Resources.getSystem();
    final String defType = "id";
    final String defPackage = "android";

    final int dayId = system.getIdentifier("day", defType, defPackage);
    final int monthId = system.getIdentifier("month", defType, defPackage);
    final int yearId = system.getIdentifier("year", defType, defPackage);

    final NumberPicker dayPicker = (NumberPicker) datePicker.findViewById(dayId);
    final NumberPicker monthPicker = (NumberPicker) datePicker.findViewById(monthId);
    final NumberPicker yearPicker = (NumberPicker) datePicker.findViewById(yearId);

    setDividerColor(dayPicker);
    setDividerColor(monthPicker);
    setDividerColor(yearPicker);
}

private void setDividerColor(final NumberPicker picker) {
    if (picker == null) {
        return;
    }

    try {
        final Field dividerField = picker.getClass().getDeclaredField("mSelectionDivider");
        dividerField.setAccessible(true);

        final TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(R.attr.colorPrimary, outValue, true);
        final int dividerColor = outValue.data;

        dividerField.set(picker, new ColorDrawable(dividerColor));
        picker.invalidate();
    } catch (Exception e) {
    }
}

答案 6 :(得分:1)

是的,你当然可以。例如,您可以使用这些属性:

<NumberPicker
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            selectionDivider="@color/black" //The divider for making the selection area
            selectionDividerHeight="1px"//The height of the selection divider
            selectionDividersDistance="3dp"//The distance between the two selection dividers
            internalLayout="@layout/something"//The layout of the number picker.
            internalMaxHeight="5dp"//The max height of the NumberPicker (also check other variations)
            internalMinWidth="5dp" // The max width of the NumberPicker (also check other variations)
            virtualButtonPressedDrawable="@drawable/something"//The drawable for pressed virtual (increment/decrement) buttons.
            />

更新:

您可以使用此custom datepicker。它具有高度可定制性和向后竞争力。它基本上使用numberpicker,你可以使用上面的属性设置divider。

答案 7 :(得分:1)

感谢Mvj解决问题!我想我也会在时间戳上展示我的解决方案实施情况。分频器。

    TimePickerDialog ptd = new TimePickerDialog(getActivity(), R.style.PickerStyler, this, hour, minute,DateFormat.is24HourFormat(getActivity()));
    ptd.setTitle("");

    //Needs to be try catched
    Field mTimePickerField = ptd.getClass().getDeclaredField("mTimePicker");
    mTimePickerField.setAccessible(true);
    TimePicker mTimePickerInstance = (TimePicker) mTimePickerField.get(ptd);    

    LinearLayout llFirst = (LinearLayout) mTimePickerInstance.getChildAt(0);
    LinearLayout llSecond = (LinearLayout) llFirst.getChildAt(0);
    boolean continiue = false;
    NumberPicker picker = null;
    for (int i = 0; i < llSecond.getChildCount(); i++) {            
        continiue = true;
        try{picker = (NumberPicker) llSecond.getChildAt(i);
            }catch(Exception e){continiue = false;}

        if(continiue){
            Field[] pickerFields = NumberPicker.class.getDeclaredFields();
            for (Field pf : pickerFields) {
                if (pf.getName().equals("mSelectionDivider")) {
                    pf.setAccessible(true);
                    try {
                        pf.set(picker, getResources().getDrawable(R.drawable.divider));
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (NotFoundException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }