gpt4 book ai didi

java - 在 fragment 中使用外部字体

转载 作者:行者123 更新时间:2023-12-01 18:46:42 25 4
gpt4 key购买 nike

我正在尝试在 fragment 中设置 TextView 的自定义字体。我看到了一些评论,也浏览了一些 stackoverflow 问题,并尝试使用自定义字体设置字体,但出现错误。我已将字体文件放在“assets/fonts”文件夹中。

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
String fontPath = "fonts/AGENCYR.ttf";

// text view label
TextView txtGhost = (TextView) root.findViewById(R.id.featured_tourname);

// Loading Font Face
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), fontPath);

// Applying font
txtGhost.setTypeface(tf);
return root;
}

这是我的 logcat 输出:

07-06 18:10:47.834: E/Trace(1372): error opening trace file: No such file or directory (2)
07-06 18:11:11.122: E/AndroidRuntime(1372): FATAL EXCEPTION: main
07-06 18:11:11.122: E/AndroidRuntime(1372): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.travelplanner/com.example.travelplanner.FeaturedTourActivity}: java.lang.RuntimeException: native typeface cannot be made
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.os.Looper.loop(Looper.java:137)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-06 18:11:11.122: E/AndroidRuntime(1372): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 18:11:11.122: E/AndroidRuntime(1372): at java.lang.reflect.Method.invoke(Method.java:511)
07-06 18:11:11.122: E/AndroidRuntime(1372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-06 18:11:11.122: E/AndroidRuntime(1372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-06 18:11:11.122: E/AndroidRuntime(1372): at dalvik.system.NativeStart.main(Native Method)
07-06 18:11:11.122: E/AndroidRuntime(1372): Caused by: java.lang.RuntimeException: native typeface cannot be made
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.graphics.Typeface.<init>(Typeface.java:175)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.graphics.Typeface.createFromAsset(Typeface.java:149)
07-06 18:11:11.122: E/AndroidRuntime(1372): at com.example.travelplanner.FragmentOne.onCreateView(FragmentOne.java:29)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.Activity.performStart(Activity.java:5114)
07-06 18:11:11.122: E/AndroidRuntime(1372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
07-06 18:11:11.122: E/AndroidRuntime(1372): ... 11 more

任何帮助都将不胜感激..

最佳答案

您的项目中没有 assets/fonts/AGENCYR.ttf,或者该字体由于某种原因与 Android 不兼容。如果您尝试其他字体并且该字体有效,则 AGENCYR.ttf 可能根本无法在 Android 上使用。

此外,请考虑许可。如果您没有 AGENCYR.ttf 的分发许可证,则不应将其打包到您的应用程序中。

关于java - 在 fragment 中使用外部字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17505803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com