Android DatePicker Шрифт - PullRequest
5 голосов
/ 10 февраля 2012

Я знаю, что вы можете применить пользовательскую TypeFace к TextView в Android следующим образом:

TextView tv = findViewById(R.id.textview01);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BLACKROSE.TTF");
tv.setTypeface(tf);

Можно ли сделать это для DatePicker?

Ответы [ 5 ]

10 голосов
/ 03 ноября 2012

Вот мой код.Надеюсь это кому-нибудь пригодится.

DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
LinearLayout layout1 = (LinearLayout) datePicker.getChildAt(0);
LinearLayout layout = layout1.getChildAt(0);

// day
LinearLayout day = (LinearLayout) layout.getChildAt(0);
setNumberPicker(day);

// month
LinearLayout month = (LinearLayout) layout.getChildAt(1);
setNumberPicker(month);

// year
LinearLayout year = (LinearLayout) layout.getChildAt(2);
setNumberPicker(year);

...

private void setNumberPicker(LinearLayout ll) {
    ((ImageButton) ll.getChildAt(0)).setBackgroundResource(R.drawable.plus_button);
    ((ImageButton) ll.getChildAt(2)).setBackgroundResource(R.drawable.minus_button);

    EditText et = (EditText) ll.getChildAt(1);
    et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    et.setTypeface(youtTypeface);
}
5 голосов
/ 10 февраля 2012

После просмотра источника виджет Datepicker содержит 3 виджета NumberPicker (для дня, месяца, года), которые в свою очередь содержат TextView.Поэтому вам нужно установить гарнитуру для TextView внутри NumberPickers внутри DatePicker.

Я думаю, что вам нужно будет получить источник для NumberPicker и DatePicker и изменить источник, чтобы добиться этого, проще говорябоюсь, чем закончить.

4 голосов
/ 22 июля 2015

Вот мое решение:

private void overrideFonts(View v) {
    ViewGroup picker;
    try {
        picker = (DatePicker) v;
    } catch (Exception e) {
        picker = (TimePicker) v;
    }
    LinearLayout layout1 = (LinearLayout) picker.getChildAt(0);
    if (picker instanceof TimePicker) {
        if (layout1.getChildAt(1) instanceof NumberPicker) {
            NumberPicker v1 = (NumberPicker) layout1.getChildAt(1);
            final int count = v1.getChildCount();
            for (int i = 0; i < count; i++) {
                View child = v1.getChildAt(i);

                try {
                    Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
                    wheelpaint_field.setAccessible(true);
                    ((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
                    ((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
                    ((EditText) child).setTypeface(// your font here);
                    v1.invalidate();
                } catch (Exception e) {
                    //TODO catch.
                    //If java cant find field then it will catch here and app wont crash.
                }
            }
        }
    }
    LinearLayout layout = (LinearLayout) layout1.getChildAt(0);
    for (int j = 0; j < 3; j++) {
        try {
            if (layout.getChildAt(j) instanceof NumberPicker) {
                NumberPicker v1 = (NumberPicker) layout.getChildAt(j);
                final int count = v1.getChildCount();
                for (int i = 0; i < count; i++) {
                    View child = v1.getChildAt(i);

                    try {
                        Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
                        wheelpaint_field.setAccessible(true);
                        ((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
                        ((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
                        ((EditText) child).setTypeface(//your font here);
                        v1.invalidate();
                    } catch (Exception e) {
                        //TODO catch.
                        //If java cant find field then it will catch here and app wont crash.
                    }
                }
            }
        } catch (Exception e) {
            //TODO catch.
            //If java cant find field then it will catch here and app wont crash.
        }
    }

}
0 голосов
/ 28 октября 2017

Kotlin-Anko, эквивалентный решению SKG

        startTimePicker = timePicker {
            this.applyRecursively {
                when(it) {
                    is NumberPicker -> {
                        val paintField = it.javaClass.getDeclaredField("mSelectorWheelPaint")
                        paintField.isAccessible = true
                        (paintField.get(it) as? Paint)?.typeface = //your font here
                    }
                    is TextView -> {
                        it.typeface = //your font here
                    }
                }
            }
0 голосов
/ 15 августа 2016

Если вы можете использовать DatePickerDialog, то вы можете просто получить кнопки диалога, например:

DatePickerDialog datePickerDialog; // GET your dialog
datePickerDialog.show();
datePickerDialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setTextSize(30); // 30 is your text size
datePickerDialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextSize(30);
...