如何更改 Android DatePicker 对话框的“分隔符”颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12711212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:30:23  来源:igfitidea点击:

How to change Android DatePicker Dialog's "Dividers" Colors

androiddatepickerandroid-4.0-ice-cream-sandwich

提问by Robin.v

I'm trying to create a custom dialog, basically I'm using DatePickerDialog.THEME_HOLO_DARKbut I want to change the "divider" color and the text color.

我正在尝试创建一个自定义对话框,基本上我正在使用DatePickerDialog.THEME_HOLO_DARK但我想更改“分隔符”颜色和文本颜色。

enter image description here

在此处输入图片说明

I want to change the blue lines and the text color to red.

我想将蓝线和文本颜色更改为红色。

Thanks in advance!

提前致谢!

EDIT:

编辑:

Using this code:

使用此代码:

<style name="testo" parent="@android:style/Widget.DeviceDefault.DatePicker">
    <item name="android:divider">@drawable/dialog_divider</item>
</style>

This is what I get: enter image description here

这就是我得到的: 在此处输入图片说明

The drawable for the divider is basically a red line..

分隔线的drawable基本上是一条红线..

回答by ahodder

You will have to create your own theme that extends THEME_HOLO_DARK. Change basic theme color of android application

您必须创建自己的主题来扩展 THEME_HOLO_DARK。更改android应用程序的基本主题颜色

Edit: Try something like this

编辑:尝试这样的事情

<style name="testo" parent="@android:style/Widget.DeviceDefault.DatePicker">
    <item name="android:divider">@drawable/MyDivider</item>
</style>

And pass the reference to your theme to the constructor

并将对您的主题的引用传递给构造函数

回答by Oleg Koshkin

add this item to your main theme

将此项目添加到您的主题

<item name="numberPickerStyle">@style/MyApp.NumberPicker</item>

set custom divider color in this style

以这种风格设置自定义分隔线颜色

<style name="MyApp.NumberPicker" parent="Widget.Holo.NumberPicker">
   <item name="selectionDivider">@color/white</item>
</style>

EDIT : tested only on HoloEverywhere

编辑:仅在 HoloEverywhere 上测试

回答by Busata

I know it's a late reply but I would like to share my findings with those who experienced or will experience this problem in the future.

我知道这是一个迟到的回复,但我想与那些经历过或将来会遇到这个问题的人分享我的发现。

I searched many places, and the only solution I found was SimonVT's Pickers.

我搜索了很多地方,我找到的唯一解决方案是 SimonVT 的 Pickers。

After implementing the library, it was very easy to change the colors. In numberpicker- the library uses images that can be simply replaced with the desired designs.

实现库后,更改颜色非常容易。在 numberpicker 中,库使用可以简单地替换为所需设计的图像。

This example has three pickers: NumberPicker, DatePicker and TimerPicker.

此示例具有三个选择器:NumberPicker、DatePicker 和 TimerPicker。

https://github.com/rodrigobusata/android-pickers

https://github.com/rodrigobusata/android-pickers

I hope you find it useful.

希望对你有帮助。


UPDATED


更新

The above link do not existe anymore, now you must use Material Design https://github.com/wdullaer/MaterialDateTimePicker.

上面的链接不再存在,现在你必须使用 Material Design https://github.com/wdullaer/MaterialDateTimePicker

回答by miteshpithadiya

Please use the below code

请使用以下代码

Here, dialog is an object of DatePickerDialog.

这里,dialog 是 DatePickerDialog 的一个对象。

    int dividerId = dialog.getContext().getResources().getIdentifier
            ("android:id/titleDivider", null, null);
    View divider = dialog.findViewById(dividerId);
    divider.setBackgroundColor(getResources().getColor(R.color.red));

    int textViewId = dialog.getContext().getResources().getIdentifier
            ("android:id/alertTitle", null, null);
    TextView tv = (TextView) dialog.findViewById(textViewId);
    tv.setTextColor(getResources().getColor(R.color.red));

回答by Rambabu Padimi

     fun applyStyLing(timePickerDialog: DatePickerDialog,context: Context) {

        try {
            var dividerId = timePickerDialog.getContext().getResources().getIdentifier("titleDivider", "id", "android")
            val divider = timePickerDialog.findViewById<View>(dividerId)
            divider.setBackgroundColor(ContextCompat.getColor(context, R.color.colorDatePicker))

            var dividerId1 = timePickerDialog.getContext().getResources().getIdentifier("alertTitle", "id", "android")
            val divider1 = timePickerDialog.findViewById<TextView>(dividerId1)
            divider1.setTextColor(ContextCompat.getColor(context, R.color.colorDatePicker))

            val system = Resources.getSystem()
            val hourNumberPickerId = system.getIdentifier("day", "id", "android")
            val minuteNumberPickerId = system.getIdentifier("month", "id", "android")
            val ampmNumberPickerId = system.getIdentifier("year", "id", "android")

            val hourNumberPicker = timePickerDialog.findViewById<View>(hourNumberPickerId) as NumberPicker
            val minuteNumberPicker = timePickerDialog.findViewById<View>(minuteNumberPickerId) as NumberPicker
            val ampmNumberPicker = timePickerDialog.findViewById<View>(ampmNumberPickerId) as NumberPicker

            setNumberPickerDividerColour(hourNumberPicker,context)
            setNumberPickerDividerColour(minuteNumberPicker,context)
            setNumberPickerDividerColour(ampmNumberPicker,context)

        }catch (e:Exception)
        {
            e.printStackTrace()
        }

    }


private fun setNumberPickerDividerColour(number_picker: NumberPicker,context: Context) {
        val count = number_picker.childCount

        for (i in 0 until count) {

            try {
                val dividerField = number_picker.javaClass.getDeclaredField("mSelectionDivider")
                dividerField.isAccessible = true
                val colorDrawable = ColorDrawable(context.getResources().getColor(R.color.colorDatePicker))
                dividerField.set(number_picker, colorDrawable)

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

        }
    }

回答by MoLo

you can change the text color by doing something like

您可以通过执行类似的操作来更改文本颜色

<style name="testo" parent="@android:style/Widget.DeviceDefault.DatePicker">

<style name="testo" parent="@android:style/Widget.DeviceDefault.DatePicker">

<item name="android:textColor">#FF0000</item>

<item name="android:textColor">#FF0000</item>

</style>

</style>

Did you ever figure out how to change the divider color? I'm trying to do the same thing and can't really find anything at the moment.

你有没有想过如何改变分隔线的颜色?我正在尝试做同样的事情,目前无法真正找到任何东西。