gpt4 book ai didi

Android更改应用程序的字体类型?

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:51 25 4
gpt4 key购买 nike

我的应用程序中需要字体字体 Helvetica 并且适用于所有 Activity 。有任何来源吗?

我在 Assets 文件夹中有 Helvetica.ttf 文件,但是当我尝试时

    TextView text2 =(TextView)findViewById(R.id.textView2);
Typeface font = Typeface.createFromAsset(getAssets(), "helvetica.ttf");
text2.setTypeface(font);

但错误是::

    10-22 17:48:43.883: ERROR/AndroidRuntime(951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.logistics.typeface/com.logistics.typeface.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.os.Looper.loop(Looper.java:123)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at java.lang.reflect.Method.invoke(Method.java:521)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at dalvik.system.NativeStart.main(Native Method)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): Caused by: java.lang.RuntimeException: native typeface cannot be made
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.graphics.Typeface.<init>(Typeface.java:147)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at com.logistics.typeface.MainActivity.onCreate(MainActivity.java:16)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-22 17:48:43.883: ERROR/AndroidRuntime(951): ... 11 more

最佳答案

在资源中定义字体样式,并在您创建元素的任何地方使用。像这样:

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>

您也可以引用这些链接:

http://developer.android.com/guide/topics/ui/themes.html

http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

如果你想使用自定义字体,那么看看这个:

http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/

关于Android更改应用程序的字体类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858860/

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