204
CHAPTER 9: User Interface
To add fonts
with different font styles, say you have the fonts myfont_regular.ttf,
myfont_bold.ttf , myfont_italic.ttf, and myfont_bold_italic.ttf inside the font resource
folder. Add the file myfont.xml by choosing New ➤ Font resource file. In this file write the
following:
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto">
android:font
app:font
android:fontWeight="400"
app:fontWeight="400"
android:font="@font/myfont_regular"
app:font="@font/myfont_regular"/>
android:font
app:font
android:fontWeight="700"
app:fontWeight="700"
android:font="@font/myfont_bold"
app:font="@font/myfont_bold"/>
android:font
app:font
android:fontWeight="400"
app:fontWeight="400"
android:font="@font/myfont_italic"
app:font="@font/myfont_italic"/>
android:font
app:font
android:fontWeight="700"
app:fontWeight="700"
android:font="@font/myfont_bold_italic"
app:font="@font/myfont_bold_italic"/>
Ignore Android Studio’s version warning;
for compatibility, all attributes use a standard and
additionally a compatibility namespace.
Then you can use the name of this XML file, without the suffix,
for UI views inside the
android:fontFamily attribute.
android:fontFamily="@font/myfont"
android:text
/>
As textStyle you could now also use italic or bold or bold|italic.