Android DatePicker 字体

发布于 2025-01-04 17:04:53 字数 287 浏览 7 评论 0原文

我知道您可以将自定义 TypeFace 应用于 Android 中的 TextView,如下所示:

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

Is there any way possible to do this for a DatePicker?

I am aware you can apply a custom TypeFace to a TextView in Android like this:

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

Is there any way possible to do this for a DatePicker?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤独患者 2025-01-11 17:04:53

这是我的代码。我希望它对某人有用。

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

Here is my code. I hope it will be useful to someone.

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);
}
孤者何惧 2025-01-11 17:04:53

查看源代码后,Datepicker 小部件包含 3 个 NumberPicker 小部件(分别代表日、月、年),而这些小部件又包含一个 TextView。因此,您必须在 DatePicker 内的 NumberPickers 内设置 TextView 的字体。

我认为您必须获取 NumberPicker 和 DatePicker 的源代码并修改源代码才能实现此目的,恐怕说起来容易做起来难。

After taking a look at the source, the Datepicker widget holds 3 NumberPicker widgets (for day, month, year)which in turn hold a TextView. So you are going have to set the Typeface for the TextView inside the NumberPickers inside the DatePicker.

I think you'll have to get the source for both NumberPicker and DatePicker and modify the source to achieve this, easier said than done I'm afraid.

亽野灬性zι浪 2025-01-11 17:04:53

这是我的解决方案:

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

}

Here is my solution:

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

}
滥情空心 2025-01-11 17:04:53

如果您可以使用 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);

If you can use DatePickerDialog, then u can simply get the buttons of the dialog, like:

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);
探春 2025-01-11 17:04:53

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

Kotlin-Anko equivalent to SKG's solution

        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
                    }
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文