Change DatePicker header text color

元气小坏坏 提交于 2019-12-17 19:52:36

问题


I have a datepicker that's showing a header on Lollipop, it looks like this

I want to either change the color of the big date in the header from black to white, or remove the header altogether, I don't care which. I've tried changing textColorPrimary, textColor and titleTextColor but it has no effect.


回答1:


Well I have no clue what's the parent theme that you have for your Date Picker theme. Let's say you have a custom theme for your Date Picker like below,

 <style name="yourCustomStyle" parent="Theme.AppCompat.Light">

Now Ctrl + click on Theme.AppCompat.Light which leads you to a new way ;) where you can find what you are looking for meaning here your issue is only about the head text but you might wish to change another views color, so that's the place you need to have a look.

And as the answer create a custom theme like below and add this attribute with the color you like

android:textColorPrimaryInverse

should do the trick for you.

   <style name="yourCustomStyle" parent="Theme.AppCompat.Light">
        <item name="colorAccent">@color/blue</item>
        <item name="android:textColorPrimaryInverse">@color/yellow</item>
        .. other
    </style>

Feel free to use your own colors and code(I copied code from this) and it will do the job!

new DatePickerDialog(MainActivity.this, R.style.yourCustomStyle, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

    }
}, 2015, 02, 26).show();

Image : android:textColorPrimaryInverse with Theme.AppCompat.Dialog




回答2:


I am able to change the date color in the header giving a custom theme CustomDatePickerDialogTheme to my datePicker DialogFragment:

<style name="CustomDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:datePickerStyle">@style/CustomDatePickerStyle</item>
</style>

<style name="CustomDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
    <item name="android:headerMonthTextAppearance">@style/HeaderTextStyle</item>
</style>

<style name="HeaderTextStyle" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor">@color/colorAccent</item>
</style>



回答3:


Try this, it might help you:

Edit your styles.xml as:

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
       <item name="colorAccent">@color/white</item>
</style>

And add below lines to your code:

new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    //DO SOMETHING
  }
}, 2015, 02, 26).show();


来源:https://stackoverflow.com/questions/42367430/change-datepicker-header-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!