android studio 找不到自定义字体文件

android studio cannot find custom font file

我在我的 activity 中使用了一个简单的 TextView,并想让它以自定义字体 'Black Chancery' 显示。我已将 blkchcry.ttf 文件放在 app>src>main>assets>fonts>blkchcry.ttf 位置。但是,android 工作室找不到该字体。这是我的 AboutusActivity.java 代码

public class AboutusActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aboutus);

    TextView tx = (TextView)findViewById(R.id.title);
    Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/blkchcry.ttf");
    tx.setTypeface(custom_font);
    }
}

下面是activity_aboutus.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AboutusActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="About Rathi Classes"
        android:textSize="20sp"
        android:layout_margin="10dp"
        android:gravity="center_horizontal"/>

    <TextView
        android:layout_below="@+id/title"
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/about_text"
        android:textSize="20sp"
        android:layout_margin="10dp" />

</RelativeLayout>

这是 logcat

Process: net.softglobe.rathiclasses, PID: 8894
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.softglobe.rathiclasses/net.softglobe.rathiclasses.AboutusActivity}: java.lang.RuntimeException: Font asset not found fonts/blkchcry.ttf
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.RuntimeException: Font asset not found fonts/blkchcry.ttf
    at android.graphics.Typeface.createFromAsset(Typeface.java:206)
    at net.softglobe.rathiclasses.AboutusActivity.onCreate(AboutusActivity.java:17)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我已尝试清理项目,但未能解决问题。请帮忙。

更改您的位置:

来自

app>src>main>assets>fonts>blkchcry.ttf.

app>src>main>res>font>blkchry.tff

然后更改字体:

 Typeface typeface = getResources().getFont(R.font.blkchcry); textView.setTypeface(typeface); 

查看更多详情:https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

更改您的位置 blkchcry.tttf 来自

app>src>main>assets>fonts>blkchcry.ttf.

app>src>main>res>font>blkchry.tff

这是有趣的部分, 您需要在同一文件夹中创建自定义字体文件。 将其命名为 my_font.xml

这就是所谓的创建字体系列

如果您使用同一字体的多个版本,那么您可能希望将它们组合到一个字体系列中。字体系列本质上是一个专用的 XML 文件,您可以在其中定义字体的每个版本及其关联的样式和粗细属性。

这是一个演示字体文件

[Update] 你可以直接粘贴因为我更新了内容。

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <font
        android:font="@font/blkchry"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/blkchry"
        app:fontStyle="normal"
        app:fontWeight="400"
        tools:ignore="UnusedAttribute" />

</font-family>

因为 getAssets() 将查找 .xml 文件,而不是 .tff,即使您在代码中提到了 .tff 文件。

像这样在此处粘贴字体,并使您的字体名称小写 :-

app  > src > main > assets > fonts > blkchry.tff

在 java 文件中设置字体,如下所示:-

public class AboutusActivity extends Activity {
 Context mContext;
 Typeface fontblkchry;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.activity_about_us);

      fontblkchry = Typeface.createFromAsset(mContext.getAssets(), "fonts/blkchry.ttf");
      textview1.setTypeface(fontblkchry);
    }
}

在我的例子中,我添加了一种带有大写字母和一些符号(如 -(连字符))的字体。 尝试重命名字体。 所以,只需将所有字母变小,用下划线替换连字符 _