从API 25迁移到26时出现字体错误

问题描述 投票:-1回答:2

目前我正在将我的项目从targetSdkVersion 25迁移到26这是我的app.gradle:

android {
    compileSdkVersion 26
    buildToolsVersion "27.0.2"

    defaultConfig {
        minSdkVersion 17

        targetSdkVersion 26

        multiDexEnabled true

        versionCode versionMajor * 100000 + versionMinor * 1000 + versionPatch * 10 + versionHotfix
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
    }
}

但升级时我反复遇到这个错误:

String types not allowed (at 'font' with value 'Knockout-31').
Message{kind=ERROR, text=String types not allowed (at 'font' with value 'Knockout-31')., sources=[D:\Qdoba-Android\qdoba-android-app\app\build\intermediates\res\merged\internal\debug\values\values.xml:2466:21-32], original message=, tool name=Optional.of(AAPT)}

这是一个字体文件夹片段:

enter image description here

请帮忙。

android android-8.0-oreo
2个回答
0
投票

根据Android文档link

Android 8.0(API级别26)引入了一项新功能,即XML中的字体,它允许您将字体用作资源。您可以在res / font /文件夹中添加字体文件以将字体捆绑为资源。这些字体在您的R文件中编译,并在Android Studio中自动提供。您可以借助新的资源类型字体访问字体资源。例如,要访问字体资源,请使用@ font / myfont或R.font.myfont。

因此,您必须将所有字体移动到res中的font文件夹中。如果找不到字体目录,请在res目录中创建一个新的“font”目录。

在您的视图中使用字体,如下所示,

android:fontFamily="@font/Knockout-31"

另一种使用自定义字体系列而不使用属性android的方法:fontFamily =“...”

  1. 对于文本视图 public class customFont extends android.support.v7.widget.AppCompatTextView{ public customFont(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public customFont(Context context, AttributeSet attrs) { super(context, attrs); init(); } public customFont(Context context) { super(context); init(); } public void init() { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Knockout-31.otf"); setTypeface(tf ,1); } }

您的文本视图与自定义字体

    <example.com.example.customFont
         android:id="@+id/textView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Text View" />

谢谢


0
投票

解决方案:更新3.0以上的gradle插件

© www.soinside.com 2019 - 2024. All rights reserved.