gpt4 book ai didi

java - 更改 DatePicker 标题文本颜色 Android

转载 作者:行者123 更新时间:2023-12-02 02:17:43 33 4
gpt4 key购买 nike

我没有使用任何像 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>

在应用程序基本主题下

 <!-- 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>

关于java - 更改 DatePicker 标题文本颜色 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57292493/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com