自定义字体在Android自定义、字体、Android

2023-09-06 02:22:50 作者:お隐形于败杀之中

我想用一个自定义的字体在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

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.