自定义字体在Android [英] Custom Font on Android

查看:122
本文介绍了自定义字体在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个自定义的字体在Android应用程序,但使用自定义字体似乎是一个痛苦? 我可以设置一个自定义编程的字体将它添加到每个的TextView - >大量的样板code。我可以创建一个新的类CustomTextView TextView的扩展,并在构造函数中添加setTypeface - >的属性没有XML上下文感知的帮助了

I want to use a custom font in an Android app but using custom fonts seems to be a pain?! I can setup a custom font programmatically by adding it to each TextView -> lots of boilerplate code. I can create a new class CustomTextView extends TextView and add setTypeface in the constructor -> no xml context aware help for attributes anymore.

有另一种方式来设置自定义字体?通过XML,按风格,按主题?

Is there another way to setup a custom font? By XML, by style, by theme?

在此先感谢!

P.S。是否有可能通过XML设置下划线?

P.S. Is there a possibility to set an underline by XML?

推荐答案

您可以包括在资产/字体自定义字体,然后用code从

You can include custom fonts under assets/fonts and then using the code from

<一个href="https://github.com/browep/AndroidCustomFontWidgets/">https://github.com/browep/AndroidCustomFontWidgets/

你可以在你的XML,例如指定字体。

you can specify the font in your XML, e.g.

<com.github.browep.customfonts.view.FontableTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is in a custom font"
    app:font="MyFont-Bold.otf" />

在code支持FontableTextView和FontableButton,但它很容易扩展,如果你需要支持其他构件类型。

The code supports FontableTextView and FontableButton, but it's quite easy to extend to support other widget types if you require.

这篇关于自定义字体在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆