自定义字体不适用于棉花糖中的日期选择器

时间:2016-04-02 14:45:35

标签: android datepicker android-6.0-marshmallow custom-font

您好我通过反射为整个应用程序设置自定义字体。它的工作正常。但它不适合日期选择器。以下是我的代码。

值-V23

<style name="datepicker" parent="android:Theme.Holo.Light.Dialog">
        <item name="colorPrimary">@color/clr_main</item>
        <item name="colorPrimaryDark">@color/clr_main</item>
        <item name="android:gravity">center</item>
        <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Small</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:background">@null</item>
        <item name="colorAccent">@color/clr_main</item>
        <item name="android:typeface">normal</item>
        <item name="android:datePickerStyle">@android:style/Widget.DatePicker</item>
        <item name="android:calendarViewShown">false</item>
        <item name="android:spinnerMode">dropdown</item>
        <item name="android:fontFamily">fonts/zonapro_regular.otf</item>
        <item name="android:borderlessButtonStyle">@style/date_button_style</item>
        <item name="android:windowBackground">@android:color/white</item>
    </style>


<style name="date_button_style">
        <item name="android:textColor">@color/clr_main</item>
        <item name="android:typeface">normal</item>
        <item name="android:layout_height">35dp</item>
        <item name="android:layout_margin">0dp</item>
        <item name="android:padding">0dp</item>
        <item name="android:textAppearance">?android:textAppearanceSmall</item>
        <item name="android:background">@android:color/transparent</item>
    </style>

//通过反射设置字体

try {
            FontsOverride.setDefaultFont(this, "DEFAULT", "fonts/zonapro_regular.ttf");
            FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/zonapro_regular.ttf");
            FontsOverride.setDefaultFont(this, "SERIF", "fonts/zonapro_regular.ttf");
            FontsOverride.setDefaultFont(this, "SANS-SERIF", "fonts/zonapro_regular.ttf");
        } catch (Exception e) {
            e.printStackTrace();
        }



public class FontsOverride {

    public static void setDefaultFont(Context context,
                                      String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    protected static void replaceFont(String staticTypefaceFieldName,
                                      final Typeface newTypeface) {

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            Map<String, Typeface> newMap = new HashMap<String, Typeface>();
            newMap.put("sans-serif", newTypeface);
            try {
                final Field staticField = Typeface.class
                        .getDeclaredField("sSystemFontMap");
                staticField.setAccessible(true);
                staticField.set(null, newMap);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            try {
                final Field staticField = Typeface.class
                        .getDeclaredField(staticTypefaceFieldName);
                staticField.setAccessible(true);
                staticField.set(null, newTypeface);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

//日期选择器类

public static class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picke

            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog dialog = new DatePickerDialog(getActivity(),R.style.datepicker, this,
                    year, month, day);
            dialog.setTitle("Select date");

            new GregorianCalendar(year, month, day);

            dialog.getDatePicker().setSpinnersShown(true);
            dialog.getDatePicker().setCalendarViewShown(false);

            return dialog;// new DatePickerDialog(getActivity(), this, year,
            // month, day);
        }


        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
            try {
                String selectedDate =  checkDigit(day)+ "-" + checkDigit(month + 1) + "-"+year;

                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

                String date = df.format(Calendar.getInstance().getTime());

                Date today = df.parse(date);
                Date selDate = df.parse(selectedDate);
                etDoe.setText(selectedDate);


            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        private String checkDigit(int number) {
            return number <= 9 ? "0" + number : String.valueOf(number);
        }


    }

0 个答案:

没有答案