Android-шрифты неожиданно не работают - PullRequest
0 голосов
/ 12 сентября 2018

Я использую рисованные шрифты уже много лет.Он используется, помещая шрифт ttf в res / font / fontname.ttf, а затем реализуя его в TextViews:

   <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:text="Hello there!"
            android:fontFamily="@font/sansregular"/>

Внезапно все мои приложения (новые и ранее установленные) перестали использовать предоставленный шрифт.

Почему это случилось?Спасибо

Ответы [ 2 ]

0 голосов
/ 12 сентября 2018

Создайте XML-файл семейства шрифтов в папке шрифтов, а затем создайте соответствующий стиль и используйте стиль в TextView.

font_family.xml:

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

    <!--Roboto-->

    <font
        android:font="@font/roboto_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/roboto_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <font
        android:font="@font/roboto_medium"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/roboto_medium"
        app:fontStyle="normal"
        app:fontWeight="400" />

</font-family>

style.xml :

<style name="TextViewStyleRegular">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:singleLine">true</item>
    <item name="android:textSize">@dimen/_12sdp</item>
    <item name="android:ellipsize">end</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:gravity">left|center_vertical</item>
    <item name="android:fontFamily">@font/roboto_regular</item>
</style>

<style name="TextViewStyleMedium" parent="TextViewStyleRegular">
    <item name="android:fontFamily">@font/roboto_medium</item>
</style>

Надеюсь, вы сможете это сделать!

0 голосов
/ 12 сентября 2018

Это нормально работает!

styles.xml

    <style name="txtLightFont" parent="android:style/Widget">
        <item name="android:fontFamily">@font/sansregular</item>
    </style>

    <style name="txtRomanFont" parent="android:style/Widget">
        <item name="android:fontFamily">@font/sansregular</item>
    </style>

    <style name="txtMediumFont" parent="android:style/Widget">
        <item name="android:fontFamily">@font/sansregular</item>
    </style>

layout.xml

<TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:text="Hello there!"
            style="@style/txtLightFont"
            />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...