简体   繁体   中英

android fonts in xml - how to override default font like calligraphy library does

I am trying to swap out calligraphy (very useful library) for android fonts in xml but i dont know how to change the default font for all views like with calligraphy. Calligraphy was able to change the default font at initialization time. How can i do this with android fonts support library ?

from the fonts in xml article on android developer site i see we can apply a font family to one view like this:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/lobster"/>

but what if i want to change the font of the entire app ?

the reason i'd like to use android xml fonts is due to this error but this is not really relevant to the question.

With the Support Library 26.1.0 I managed to have a default font for the whole app by setting the android:fontFamily attribute for the application theme:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
     ...
    <item name="android:fontFamily">@font/proxima_nova</item>
</style>

Where proxima_nova.xml is a font resource file:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedAttribute">
    <!-- As of Android Support Library 26.0, we must declare both sets of attributes to ensure
         our fonts load on devices running Android 8.0 (API level 26) or lower. -->
    <font
        android:font="@font/proxima_nova_light"
        android:fontStyle="normal"
        android:fontWeight="300"
        app:font="@font/proxima_nova_light"
        app:fontStyle="normal"
        app:fontWeight="300" />
    <font
        android:font="@font/proxima_nova_semibold"
        android:fontWeight="600"
        app:font="@font/proxima_nova_semibold"
        app:fontWeight="600" />
</font-family>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM