Using font in XML in API < 26

后端 未结 5 1002
旧时难觅i
旧时难觅i 2020-12-09 08:02

https://developer.android.com/preview/features/working-with-fonts.html

Android API 26 supports this but do we have a support library that can provide use of fonts as

5条回答
  •  心在旅途
    2020-12-09 08:51

    Some users will land up to this page searching for instructions to use custom fonts in your app from xml. Here's how I did it:

    The font resource directory is not present by default. You can create it manually by following the documentation here [too much work]

    Or use font selector from GUI-

    • Select a TextView in Design tab.

    • Open drop down of fontFamily xml attribute.

    • Scroll down to "More Fonts".

    • Choose a 'Downloadable font" from the pop up that appears.

    Android Studio will automatically create font resource directory with necessary declarations mentioned in documentation above. After this step you can copy your custom font file (say myfont.ttf) in font directory and set your desired font from xml for example:

    
    

    If you want to use the same font throughout your app, you can set fontFamily your AppTheme in styles.xml:

    
    

    [ prefix fontFamily with android: if above did not work ]

    Screenshots:

    Note: This answer assumes you are using Android Studio 3+ and support library version 26+

提交回复
热议问题