Material Date Picker Custom Styling

前端 未结 4 847
清酒与你
清酒与你 2021-02-09 03:18

i have used range date picker from google material with this library

implementation \'com.google.android.material:material:1.2.0-alpha02\'

4条回答
  •  一生所求
    2021-02-09 04:17

    While the posted answer totally work there seems to be no need to set the materialCalendarTheme globally - you can just set it to via the MaterialDatePicker.Builder and setTheme(int themeResId) method. Following an example how they do it in the Material Design Catalog App.

    val datePicker = MaterialDatePicker.Builder.dateRangePicker().apply {
        context?.resolveOrNull(R.attr.materialCalendarTheme)?.let {
            setTheme(it)
        }
        setCalendarConstraints(getConstraints())
        }.build()
    // ...
    

    resolveOrThrow helper method:

    fun Context.resolveOrNull(@AttrRes attributeResId: Int): Int? {
        val typedValue = TypedValue()
        if (theme.resolveAttribute(attributeResId, typedValue, true)) {
            return typedValue.data
        }
        return null
    }
    

    This way you DatePicker dialog won't be full screen but a regular dialog.

提交回复
热议问题