Android Studio 3.0 custom font in styles.xml

I cannot set a custom font in a style file.

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:textViewStyle">@style/fontTextView</item>
        ...
    </style>

    <style name="fontTextView" parent="@android:style/Widget.TextView">
        <item name="android:fontFamily">@font/example</item>
    </style>
</resources>

and I still see the default font in my application. But when I change fontFamilyto one of the defaults, it changes

    <style name="fontTextView" parent="@android:style/Widget.TextView">
        <item name="android:fontFamily">monospace</item>
    </style>

It also works if I install the software font programmatically.

setTypeface(ResourcesCompat.getFont(context, R.font.example));

How to make it work for my custom font in styles.xml?

I installed compiltSdkVersionin 26and buildToolsVersionin 26.0.2. I also use the support library:

  def supportVersion = '26.1.0'
  implementation "com.android.support:appcompat-v7:$supportVersion"

My font family:

<?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/example_regular"

    app:fontStyle="normal"
    app:fontWeight="300"
    app:font="@font/example_regular"/>

<font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="normal"
    app:fontWeight="400"
    app:font="@font/example_regular"/>

<font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="normal"
    app:fontWeight="700"
    app:font="@font/example_regular"/>

<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="italic"
    app:fontWeight="300"
    app:font="@font/example_regular" />

<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="italic"
    app:fontWeight="400"
    app:font="@font/example_regular" />

<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="italic"
    app:fontWeight="700"
    app:font="@font/example_regular" />

</font-family>

I use the same file *ttffor testing purposes.

+4
source share
2 answers

Ok. - ... Activity AppCompatActivity

+1

xml-:   (.ttf) . fontFamily xml-.  

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="40dp"
    android:fontFamily="@font/semibold"/>

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

0

Source: https://habr.com/ru/post/1688058/


All Articles