【问题标题】:Styling DatePicker Android Marshmallow样式化 DatePicker Android Marshmallow
【发布时间】:2016-05-04 01:12:19
【问题描述】:

我正在为 android 日历样式而苦苦挣扎。事实证明,android 6 忽略了 calendarTextColor 并使用 textColorPrimary 来设置日期标签的样式,并使用 textColorSecondary 来设置星期几的样式。我在 android 5 上检查了 calendarTextColor,它工作正常。根据文档 textColorPrimary 用作工具栏文本颜色。 https://developer.android.com/training/material/theme.html 所以我的工具栏文本颜色是白色的,我收到白色背景上的白色情人节标签。如何在不触摸 android api 23 的 textColorPrimary 的情况下指定 calendarTextColor?

【问题讨论】:

    标签: android datepicker styling android-6.0-marshmallow


    【解决方案1】:

    您可以为日期选择器制作自定义主题并在那里指定文本颜色

    试试这个

    <style name="datePickerTheme" 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:textColorPrimary">@color/colorBlack</item>
        <item name="android:textColorSecondary">@color/colorBlack</item>
        <item name="android:textColorTertiary">@color/colorBlack</item>
    
    </style>
    

    然后您可以在像这样调用 DatePickerDialog 时指定此主题

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
    
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "MM/dd/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                calendar.setText(sdf.format(myCalendar.getTime()));
            }
    
        };
    
        calendar.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(YourActivity.this,R.style.datePickerTheme, date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        }); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 2023-03-30
      相关资源
      最近更新 更多