繁体   English   中英

更改Android的DatePicker标头文本颜色

[英]Change DatePicker header text color Android

我没有使用像MaterialDialog之类的任何库,而是在自定义对话框中使用了Android的默认DatePicker 我想要的是更改标题背景和文本。 背景(透明)有效,但文本无效。 到目前为止,这就是我所做的:

date_time_dialog_picker.xml布局中的DatePicker

<DatePicker
            android:id="@+id/datePickerDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:calendarViewShown="true"
            android:datePickerMode="calendar"
            android:layout_gravity="center_horizontal"
            android:endYear="2100"
            android:maxDate="12/31/2100"
            android:minDate="01/01/2019"
            android:spinnersShown="true"
            style="@style/CalendarDatePickerDialog"
            android:startYear="2019" />

样式

<style name="CalendarDatePickerDialog" parent="Theme.AppCompat.Light">
        <item name="android:headerBackground">@android:color/transparent</item>
        <item name="android:colorControlNormal">@color/colorPrimary</item>
        <item name="android:colorControlHighlight">@color/colorPrimary</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:textColorPrimaryInverse">@color/colorPrimaryDark</item> <!-- header date, month color && calendar text highlight color -->
        <item name="android:textColorSecondaryInverse">@color/colorPrimaryDark</item> <!-- header year color -->
    </style>

然后是Java代码

private void showDatePicker() {
        LayoutInflater inflater = getLayoutInflater();
        final View customView = inflater.inflate(R.layout.date_time_dialog_picker, null);
 Calendar dateTimeCalendar = Calendar.getInstance();
dateTimeCalendar.setTimeZone(TimeZone.getDefault());
year = dateTimeCalendar.get(Calendar.YEAR);
        month = dateTimeCalendar.get(Calendar.MONTH);
        day = dateTimeCalendar.get(Calendar.DAY_OF_MONTH);

 final DatePicker dpStartDate = customView.findViewById(R.id.datePickerDialog);
 dpStartDate.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
                MyActivity.this.year = year;
                MyActivity.this.month = month;
                MyActivity.this.day = day;
            }
        });

AlertDialog.Builder builder = new AlertDialog.Builder(AddPermessoActivity.this);
        builder.setView(customView); // Set the view of the dialog to your custom layout
        builder.setTitle("");
        builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "date: " + MyActivity.this.year + " " + MyActivity.this.month + " " + MyActivity.this.day);
                dialog.dismiss();
            }});

        // Create and show the dialog
        builder.create().show();
}

因此,似乎只有此属性有效

 <item name="android:headerBackground">@android:color/transparent</item>

谢谢

<item name="android:textColorPrimaryInverse">@color/colorPrimary</item>

在App base主题下

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

     <!-- header date, month color && calendar text highlight color -->
    <item name="android:textColorPrimaryInverse">@color/colorPrimary</item> 
 </style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM