我想更改android内部的textview字体。
以下是代码:
TextView textView = (TextView) findViewById(R.id.textView);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
textView.setTypeface(font);
我把字体放在src/main/assets/fonts
文件夹中。
但是当我尝试运行应用程序(在模拟器上)时,它会崩溃。那是为什么?
编辑:这是我收到的错误消息。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.asus.wetr, PID: 3973
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asus.wetr/com.asus.wetr.MainActivity}: java.lang.RuntimeException: Font asset not found fonts/Roboto-Thin.TTF
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Font asset not found fonts/Roboto-Thin.TTF
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.asus.wetr.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.
EDIT2:这是我要求的activity.xml。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.asus.wetr.MainActivity"
android:background="@drawable/background_color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="238dp"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#FFF"/>
</RelativeLayout>
答案 0 :(得分:4)
您忘记将TextView
上下文传递给createFromAsset
类的TypeFace
方法
使用:
Typeface tf = Typeface.createFromAsset(textView.getContext()
.getAssets(), "fonts/Roboto-Thin.ttf");
我希望这会对你有所帮助。
答案 1 :(得分:1)
调查stacktrace:
未找到字体资源字体/ Roboto-Thin.TTF
看起来您的文件名不同。即使文件扩展名案例也很重要。那么,你确定文件名是否完全相同,还有扩展案例?
答案 2 :(得分:1)
我设法解决了自己的问题。伙计们,我想感谢你们所有的投入。我做了一个小的(我这是一个新手)错误,这搞砸了一切。
我把这段代码:
TextView textView = (TextView) findViewById(R.id.textView);
Typeface thisfont = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
textView.setTypeface(thisfont);
之前:
setContentView(R.layout.activity_main);
哪个不起作用(对大多数人来说显而易见),因为尚未设置布局。它必须追随。经验教训。