更改DatePicker标题文本颜色

时间:2017-02-21 12:31:26

标签: android datepicker android-styles datepickerdialog

我有一个在Lollipop上显示标题的日期选择器,它看起来像这样 Datepicker

我想要将标题中大日期的颜色从黑色更改为白色,或者完全删除标题,我不关心哪个。我尝试更改textColorPrimarytextColortitleTextColor,但它没有效果。

5 个答案:

答案 0 :(得分:5)

好吧,我不知道你的日期选择器主题有什么主题。假设您的日期选择器有自定义主题,如下所示,

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

现在 Ctrl +点击 Theme.AppCompat.Light ,这会引导您采用新的方式;)您可以在这里找到您要找的内容,这里的问题是只关于头文本,但您可能希望更改另一种视图颜色,这就是您需要查看的位置。

然后,答案会创建一个如下所示的自定义主题,并使用您喜欢的颜色添加此属性

android:textColorPrimaryInverse

应该为你做的伎俩。

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

随意使用您自己的颜色和代码(我复制了来自this的代码),它将完成这项工作!

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();

enter image description here enter image description here

图片:android:textColorPrimaryInverse with Theme.AppCompat.Dialog

答案 1 :(得分:3)

我可以更改标题中的日期颜色,为我的datePicker DialogFragment提供自定义主题CustomDatePickerDialogTheme

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

答案 2 :(得分:0)

试试这个,它可能对你有帮助:

将您的styles.xml编辑为:

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

并在代码中添加以下行:

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();

答案 3 :(得分:0)

您可以按照以下步骤更改 DateTimePicker 标题和正文背景:

  1. 转到 android/app/src/main/res/values/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="android:textColor">#000000</item>

 **Add these two files !!!**

    <item name="android:timePickerDialogTheme">@style/ClockTimePickerDialog</item>
    <item name="android:datePickerDialogTheme">@style/DialogDatePicker.Theme</item>
</style>

 **Add these files also** 

<style name="DialogDatePicker.Theme" parent="Theme.AppCompat.DayNight.Dialog">
    <item name="colorAccent">#000000</item>
</style>

<style name="ClockTimePickerDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#000000</item>
    <item name="android:textColorPrimary">#000</item>
</style>

答案 4 :(得分:0)

科特林,2021 年

// set date as button text if pressed
btnDate.setOnClickListener(View.OnClickListener {

    val dpd = DatePickerDialog(
        this,
        { view, year, monthOfYear, dayOfMonth ->
            val selectDate = Calendar.getInstance()
            selectDate.set(Calendar.YEAR, year)
            selectDate.set(Calendar.MONTH, monthOfYear)
            selectDate.set(Calendar.DAY_OF_MONTH, dayOfMonth)

            var formatDate = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
            val date = formatDate.format(selectDate.time)
            Toast.makeText(this, date, Toast.LENGTH_SHORT).show()
            btnDate.text = date
        }, 1990, 6, 6
    )

    val calendar = Calendar.getInstance()
    val year = calendar[Calendar.YEAR]
    val month = calendar[Calendar.MONTH]
    val day = calendar[Calendar.DAY_OF_MONTH]
    dpd.datePicker.minDate = GregorianCalendar(year - 90, month, day, 0, 0).timeInMillis
    dpd.datePicker.maxDate = GregorianCalendar(year - 10, month, day, 0, 0).timeInMillis
    dpd.show()
})

Styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- This is main Theme Style for your application! -->
    <item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>

<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Dialog">
    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
</style>

<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.DatePicker">
    <item name="android:headerBackground">#A500FF</item>
</style>

enter image description here