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

标签 android fonts android-support-library android-studio-3.0

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

<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 文件进行测试。

最佳答案

好的。我解决了这个问题 - 这种行为的原因很简单......我正在扩展 Activity 而不是 AppCompatActivity

关于styles.xml 中的 Android Studio 3.0 自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46905310/

相关文章:

java - 在 JEditorPane 中获取选定文本的字体

css - 基于字符或数字数据的字体大小

android - 将 ActionBar 区域的颜色更改为绿色

c# - Xamarin Android 支持 v7 错误

java - 底部导航栏下方的空间

android - 有人可以向我解释制作 sqlcipher 二进制文件的编译步骤吗?

java - 没有 UI 的 Android 后台服务没有应答

android - 文本输入布局 : Different color for hint label when not focused

android - 在我的应用程序中设置自定义文本样式时出错

android - ConstraintLayout 在 GUI 编辑器 Android Studio 3.0 - Canary 4 中不起作用