[英]android studio cannot find custom font file
I am using a simple TextView
in my activity and want to make it appear in a custom font 'Black Chancery'.我在我的活动中使用了一个简单的
TextView
,并想让它出现在自定义字体“Black Chancery”中。 I have put the blkchcry.ttf
file at location app>src>main>assets>fonts>blkchcry.ttf .我已将
blkchcry.ttf
文件放在app>src>main>assets>fonts>blkchcry.ttf 位置。 But, the android studio can't find that font.但是,android studio 找不到那个字体。 Here's my
AboutusActivity.java
code这是我的
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);
}
}
and following is the activity_aboutus.xml
以下是
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>
and here is the logcat这是 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)
I have tried cleaning the project but it failed to resolve the problem.我曾尝试清理项目,但未能解决问题。 Please help.
请帮忙。
Change the location your blkchcry.tttf
from更改您的
blkchcry.tttf
的位置
app>src>main>assets>fonts>blkchcry.ttf.
to到
app>src>main>res>font>blkchry.tff
And here's the fun part, you need to create a custom font file inside the same folder.这是有趣的部分,您需要在同一个文件夹中创建一个自定义字体文件。 Name it as
my_font.xml
将其命名为
my_font.xml
That's called Creating a Font Family这就是所谓的创建字体系列
If you're using multiple versions of the same font, then you may want to group them together into a font family.如果您使用相同字体的多个版本,那么您可能希望将它们组合成一个字体系列。 A font family is essentially a dedicated XML file where you define each version of the font, along with its associated style and weight attributes.
字体系列本质上是一个专用的 XML 文件,您可以在其中定义字体的每个版本及其关联的样式和粗细属性。
Here's a demo font file这是一个演示字体文件
[ Update ] which you can simply paste coz I updated the contents. [更新] 您可以简单地粘贴,因为我更新了内容。
<?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>
Because getAssets()
will look for an .xml
file, not .tff
even though you mentioned .tff
file in your code.因为即使您在代码中提到了
.tff
文件, getAssets()
也会查找.xml
文件,而不是.tff
。
Paste Font here like this and also make your font name in small case :-像这样在此处粘贴字体,并将您的字体名称设为小写:-
app > src > main > assets > fonts > blkchry.tff
set Font in java File like this :-像这样在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);
}
}
Change your location:更改您的位置:
from从
app>src>main>assets>fonts>blkchcry.ttf.
to到
app>src>main>res>font>blkchry.tff
then change your typeface:然后改变你的字体:
Typeface typeface = getResources().getFont(R.font.blkchcry); textView.setTypeface(typeface);
See more details: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml查看更多详情: https : //developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
In my case, I was adding a font with Capital Letters and some symbols like -(hyphen).就我而言,我添加了一种带有大写字母和一些符号(如 -(连字符))的字体。 Try renaming the fonts.
尝试重命名字体。 So, just make all letters small, replace hyphen with underscore _
因此,只需将所有字母变小,用下划线 _ 替换连字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.