0

我正在尝试在片段中设置文本视图的自定义字体。我已经看到了一些 tuts 并遍历了一些 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

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

4

1 回答 1

4

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

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

于 2013-07-06T18:53:26.260 回答