【发布时间】:2018-06-25 13:00:21
【问题描述】:
我在我的活动中使用了一个简单的TextView,并希望它以自定义字体“Black Chancery”出现。我已将blkchcry.ttf 文件放在app>src>main>assets>fonts>blkchcry.ttf 位置。但是,android studio 找不到那个字体。这是我的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)
我已尝试清理项目,但未能解决问题。请帮忙。
【问题讨论】:
-
这里猜测一下,文件有特殊权限还是什么?如果您指定机器上的确切文件补丁而不是相对资产路径,它是否有效?
标签: android android-assets android-fonts