更改DatePickerDialog的标题背景

时间:2018-12-04 10:50:42

标签: android

我有一个DatePickerDialog,我想更改标题的背景颜色。我这样做是为了使整个应用程序的默认背景为白色,但是现在,由于Dialog标题中的白色文本,它不可见并且看起来很奇怪。 enter image description here

我尝试使用SO中的许多其他答案,但是没有运气,这是我在styles.xml中设置的样式

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:textSize">12dp</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:headerBackground">@color/colorPrimary</item>
</style>

非常感谢!

编辑:目前,由于我在此应用中使用的布局不多,因此我通过将每个元素的背景手动设置为白色来修复了该问题。修复仍然会很好...

2 个答案:

答案 0 :(得分:0)

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

//After replace below code to your code 

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

答案 1 :(得分:0)

//Put it in your style.xml file
 <style name="datepicker" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:fontFamily">@font/quicksand_regular</item>
    </style>


//after below code put in your view  onclickListner 

public void Datepiker()
{
    // Get Current Date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog datePickerDialog = new DatePickerDialog(this,R.style.datepicker,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {

                    txt_PickupDate_val.setText(dayOfMonth + "-" + monthOfYear  + "-" + year);

                  //  txt_PickupDate_val.setEnabled(true);
                }
            }, mYear, mMonth, mDay);
    datePickerDialog.show();
}