默認(rèn)樣式
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="calendar"
/>
image.png
修改后
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="calendar"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#fff"
style="@style/CustomDatePickerTheme"
android:theme="@style/picker" />
image.png
themes.xml文件
<style name="CustomDatePickerTheme" parent="@android:style/Widget.Material.Light.DatePicker">
<!-- 控制頭部背景色 -->
<item name="android:headerBackground">#0080ff</item>
</style>
<style name="picker" parent="android:Theme.Material">
<!-- 控制頭部年份 -->
<item name="android:textColorSecondaryInverse">#f0f</item>
<!-- 控制頭部的月磁餐、日梳凛、星期恬试,以及選中的日期的顏色 -->
<item name="android:textColorPrimaryInverse">#ff0</item>
<!-- 控制年月日的顏色,日期優(yōu)先使用 textColorPrimaryActivated -->
<item name="android:textColorPrimary">#00f</item>
<!-- 控制日期的顏色 -->
<!-- <item name="android:textColorPrimaryActivated">#f00</item>-->
<!-- 控制星期的顏色 -->
<item name="android:textColorSecondary">#0f0</item>
<!-- 左右切換年份按鈕的背景色 -->
<item name="android:selectableItemBackgroundBorderless">@drawable/rect_round_white</item>
<!-- 選中的日期的背景色衔沼,以及今天的日期的文字顏色 -->
<item name="android:colorAccent">#cc9</item>
</style>
如果想隱藏頭部(藍(lán)色背景區(qū)域)叫乌,可以這樣做
if (pickerView.childCount == 1) {
(pickerView.getChildAt(0) as? LinearLayout)?.let { container ->
if (container.childCount == 2) {
(container.getChildAt(0) as? LinearLayout)?.let { pickerHeaderView ->
pickerHeaderView.visibility = View.GONE
}
}
}
}