android datepicker样式
时间: 2023-07-22 13:20:27 浏览: 289
你可以通过自定义样式来修改 Android DatePicker 控件的外观。以下是一个示例:
1. 首先,在你的 layout 文件中,添加一个 DatePicker 控件:
```xml
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
2. 然后,在你的 styles.xml 文件中,定义一个样式:
```xml
<style name="MyDatePickerStyle" parent="@android:style/Widget.DatePicker">
<item name="android:calendarTextColor">#FFFFFF</item>
<item name="android:headerBackground">#000000</item>
<item name="android:headerDayOfMonthTextAppearance">@style/MyDatePickerTextAppearance</item>
<item name="android:headerMonthTextAppearance">@style/MyDatePickerTextAppearance</item>
<item name="android:headerYearTextAppearance">@style/MyDatePickerTextAppearance</item>
<item name="android:yearListItemTextAppearance">@style/MyDatePickerTextAppearance</item>
<item name="android:yearListSelectorColor">#FF0000</item>
</style>
<style name="MyDatePickerTextAppearance">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">20sp</item>
</style>
```
3. 最后,在你的代码中,使用这个样式:
```java
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
datePicker.setCalendarViewShown(false);
datePicker.setSpinnersShown(true);
datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
datePicker.setMaxDate(new Date().getTime());
datePicker.setMinDate(minDate.getTime());
datePicker.init(year, month, day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// 处理日期变化事件
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
datePicker.setFirstDayOfWeek(Calendar.MONDAY);
datePicker.setElevation(4f);
datePicker.setTranslationZ(2f);
datePicker.setBackgroundColor(Color.BLACK);
datePicker.setForegroundGravity(Gravity.CENTER);
datePicker.setGravity(Gravity.CENTER);
datePicker.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
datePicker.setCalendarViewShown(false);
datePicker.setSpinnersShown(true);
datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
datePicker.setMaxDate(new Date().getTime());
datePicker.setMinDate(minDate.getTime());
datePicker.setSpinnersShown(true);
datePicker.setMinHeight(200);
datePicker.setMaxHeight(500);
datePicker.setMinWidth(200);
datePicker.setMaxWidth(500);
datePicker.setSaveEnabled(true);
datePicker.setStyle(R.style.MyDatePickerStyle); // 设置样式
}
```
注意:以上代码仅供参考,具体样式和代码实现可能需要根据你的需求进行修改。
阅读全文