How to change Android DatePicker Dialog's “Dividers” Colors

前端 未结 6 1394
無奈伤痛
無奈伤痛 2021-01-04 18:50

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

6条回答
  •  心在旅途
    2021-01-04 19:22

         fun applyStyLing(timePickerDialog: DatePickerDialog,context: Context) {
    
            try {
                var dividerId = timePickerDialog.getContext().getResources().getIdentifier("titleDivider", "id", "android")
                val divider = timePickerDialog.findViewById(dividerId)
                divider.setBackgroundColor(ContextCompat.getColor(context, R.color.colorDatePicker))
    
                var dividerId1 = timePickerDialog.getContext().getResources().getIdentifier("alertTitle", "id", "android")
                val divider1 = timePickerDialog.findViewById(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(hourNumberPickerId) as NumberPicker
                val minuteNumberPicker = timePickerDialog.findViewById(minuteNumberPickerId) as NumberPicker
                val ampmNumberPicker = timePickerDialog.findViewById(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)
                }
    
            }
        }
    

提交回复
热议问题