Android Studio 3.0中styles.xml中的自定义字体

4

我的样式文件无法设置自定义字体。

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

我在我的应用程序中仍然看到默认字体。但是当我将fontFamily更改为其中一个默认字体时,它就会改变。

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

如果我通过编程设置自定义字体,它也会起作用。

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

如何在styles.xml中使用自定义字体?
我将compiltSdkVersion设置为26,并将buildToolsVersion设置为26.0.2。同时,我正在使用支持库:
  def supportVersion = '26.1.0'
  implementation "com.android.support:appcompat-v7:$supportVersion"

我的字体系列:

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

我用同一个*ttf文件进行测试。


{btsdaf} - user6490462
@Ibrahim 是的。如果我以编程方式设置它,它是有效的:setTypeface(ResourcesCompat.getFont(context, R.font.example)); - Lau
{btsdaf} - Arbaz Ali Rizvi
{btsdaf} - Arbaz Ali Rizvi
{btsdaf} - Sandeep Manmode
显示剩余2条评论
2个回答

3

好的。我解决了这个问题——这种行为的原因非常简单……我扩展了Activity,而不是AppCompatActivity


1

using custom font for xml design : First you need to create font folder in your resource directory after that put all custom font(.ttf file) file in to font folder. then just add attribute fontFamily in xml design . Like

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接