Using font in XML in API < 26

后端 未结 5 1003
旧时难觅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:46

    According to Google it is supported on Android 4.0+ (API 14+) as long as these conditions are met:

    • You are using the support library version 26.0.0-beta1 or later
    • You are using AppCompat

    Sources:

    Fonts in XML - Using the support library

    Support Library revision history

    I was hoping I could use this in app widgets on Android versions earlier than 8 (API 26), however it's not possible, as AppCompatTextView cannot be used in app widgets. None of the third party alternatives to Android O's 'Fonts in XML' such as the Caligraphy library work in app widgets either.

    The only alternative for app widgets is using an ImageView instead of a TextView and using RemoteViews methods such as setImageViewUri(...) and setImageViewBitmap(...), both of which are problematic.

    0 讨论(0)
  • 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:

    <TextView
         android:fontFamily="@font/myfont"
         android:id="@+id/aboutus_head_textview"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/who_are_we" />
    

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

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <!-- custom font for the entire app -->
            <item name="fontFamily">@font/myfont</item>
        </style>
    

    [ 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+

    0 讨论(0)
  • 2020-12-09 08:52

    Here is an example, min sdk support 18 (in app)

    https://github.com/mukundrd/androidFonts

    0 讨论(0)
  • 2020-12-09 08:57

    To support font family on API lower than 26, we need to use

    <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
    

    instead of

    <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"/>
    
    0 讨论(0)
  • 2020-12-09 09:03

    To complete Yuri's answer, Fonts are supported back to API 16. To create font family (where different weights of fonts are specified), you should create my_font_family.xml under /res/font/ as written in the doc:

    <font-family xmlns:app="http://schemas.android.com/apk/res-auto">
        <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont_regular"/>
        <font app:fontStyle="normal" app:fontWeight="600" app:font="@font/myfont_semi_bold" />
        <font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont_italic" />
    </font-family>
    

    To use this in TextView xml, you should use app:fontFamily="@font/my_font_family", be careful with app: instead of android:.

    To choose specific fontWeight, you can use android:textFontWeight="200" but it only works on API>=28. To choose different font based on weight prior to API 28, you have two options. You can either use android:textStyle="bold" to specify only "normal/italic/bold", but not exact weight. Or you can directly use specific weight of font like this: app:fontFamily="@font/myfont_semi_bold".

    Please Note: If you are using custom textview, YourTextView, in your app, it has to extend AppCompatTextView and not merely android.widget.TextView

    0 讨论(0)
提交回复
热议问题