Use fonts in Android O XML

Use fonts in Android O XML

2017/9/25 20:14:35

Android 8.0 introduced a new feature, fonts in XML, that allows you to use fonts as resources.This means that you no longer need to bundle fonts as assets.Fonts are compiled in R files and, as a resource, are automatically used on the system.You can then access these fonts using a new resource type, font.

Support library 26 fully supports this feature on devices running API versions 14 and later.(from official documents)

Effect

Implementation Steps

Create a new font folder under res

Add fonts to the font folder

Reference in XML Layout (@font)

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/aayuanqimanman"
    android:text="Bright Moonlight before bed, frost on the ground suspected.
                  //Raising my head, I see the moon so bright; withdrawing my eyes, my nostalgia comes around."
    android:textSize="28sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

You can also define a set of font file settings

Create a new resource file under the font folder

resource file

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">

    <font
        android:font="@font/aayuanqimanman"
        android:fontStyle="normal"
        android:fontWeight="100" />

    <font
        android:font="@font/aayuanqimanman"
        android:fontStyle="italic"
        android:fontWeight="100" />
</font-family>

android:fontStyle two properties

normal

italic

Operational font properties can be selected in the panel

style.xml settings

<style name="myfontstyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily" tools:targetApi="jelly_bean">@font/a_font</item>
</style>

Programming Operations

Get the font resource identity using the getFont() method only

Set font using setTypeface()

Typeface typeface = getResources().getFont(R.font.a_font);
tv_1.setTypeface(typeface);

Two sets of properties must be declared in order to be compatible with lower versions

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto">
    <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"
          app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
    <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/myfont-Italic"
          app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />
</font-family>

Retrieving font resource usage

ResourcesCompat.getFont()

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

Keywords: Android xml encoding Programming

Added by bios on Sun, 19 Apr 2020 19:35:47 +0300