简体   繁体   English

Android Studio中的自定义字体

[英]custom font in Android Studio

How to create custom fonts in android studio ? 如何在android studio中创建自定义字体? Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. 我试图使用自定义字体,我读过我想将字体放在资产/字体中。 I have searched long and hard but found nothing that helped yet. 我一直在努力寻找,但却没有找到任何帮助。 Where I wrong? 哪里错了? I really do not know what to do. 我真的不知道该怎么办。 I wrote all the details below. 我在下面写了所有细节。 I've tried and did not succeed. 我尝试过但没有成功。 I'm using Android Studio and it doesn't seem like I have a assets folder. 我正在使用Android Studio,似乎我没有资产文件夹。 So I created one. 所以我创建了一个。 But my app crashes when I place the assets folder in /main/assets. 但是当我将资源文件夹放在/ main / assets中时,我的应用程序崩溃了。 Im using this code to load my fonts. 我使用此代码加载我的字体。

I know that . 我知道 。 structure for a project in Android Studio 0.5.2: Android Studio 0.5.2中项目的结构:

root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------fonts/SomeFont.ttc
|----------fonts/AnotherFont.otf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle

Update 更新

my fonte file dir : E:\\AndroidProjects\\ReadNews\\ReadNews\\src\\main\\assets\\fonts\\ 我的fonte文件目录:E:\\ AndroidProjects \\ ReadNews \\ ReadNews \\ src \\ main \\ assets \\ fonts \\

end Update 结束更新

my code : 我的代码:

 @Override
     protected void onPreExecute() {
            TextView titleTextView = (TextView) _dialog.findViewById(R.id.title_news_TextView);
            TextView messageTextView = (TextView) _dialog.findViewById(R.id.message_TextView);
            String fontPath = "fonts/Tahoma.ttf";
            Typeface tf = Typeface.createFromAsset(MainActivity.this.getResources().getAssets(), fontPath);
            titleTextView.setTypeface(tf);
            messageTextView.setTypeface(tf);
        }

error : 错误:

06-27 07:40:37.985 4206-4206/com.mmd2009.readnews E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.mmd2009.readnews, PID: 4206 java.lang.RuntimeException: native typeface cannot be made at android.graphics.Typeface.(Typeface.java:175) at android.graphics.Typeface.createFromAsset(Typeface.java:149) at com.mmd2009.readnews.MainActivity$ReadNewsAsyncTask.onPreExecute(MainActivity.java:216) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) at android.os.AsyncTask.execute(AsyncTask.java:535) at com.mmd2009.readnews.MainActivity$1.onClick(MainActivity.java:70) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java 06-27 07:40:37.985 4206-4206 / com.mmd2009.readnews E / AndroidRuntime:FATAL EXCEPTION:main进程:com.mmd2009.readnews,PID:4206 java.lang.RuntimeException: 无法在android上创建本机字体 。在android.os上的com.mmd2009.readnews.MainActivity $ ReadNewsAsyncTask.onPreExecute(MainActivity.java:216)的android.graphics.Typeface.createFromAsset(Typeface.java:149)上的graphics.Typeface。(Typeface.java:175)。位于android.view.View的com.mmd2009.readnews.MainActivity $ 1.onClick(MainActivity.java:70)的android.os.AsyncTask.execute(AsyncTask.java:535)上的AsyncTask.executeOnExecutor(AsyncTask.java:587)。在Android.os.Handler.dispatchMessage(Handler)上的android.view.View $ PerformClick.run(View.java:18422)android.os.Handler.handleCallback(Handler.java:733)执行点击(View.java:4438) .java:95)在android.app.Looper.loop(Looper.java:136)的android.app.ActivityThread.main(ActivityThread.java:5017)at java.lang.reflect.Method.invokeNative(Native Method)at at java.lang.reflect.Method.invoke(Method.java :515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) :515)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)位于dalvik.system.NativeStart的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)。 main(原生方法)

my ReadNews-ReadNews.iml : 我的ReadNews-ReadNews.iml:

  <facet type="android" name="Android">
      <configuration>
        <option name="SELECTED_BUILD_VARIANT" value="debug" />
        <option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
        <option name="COMPILE_JAVA_TASK_NAME" value="compileDebugJava" />
        <option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugTest" />
        <option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
        <option name="ALLOW_USER_CONFIGURATION" value="false" />
        <option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
        <option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
        <option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
        <option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
      </configuration>
    </facet>

Your font path is incorrect. 您的字体路径不正确。 You have no font folder. 你没有font文件夹。 Change your font path to just "Tahoma.ttf" , or create the folder inside of your asset folder. 将您的字体路径更改为"Tahoma.ttf" ,或在资产文件夹中创建文件夹。

EDIT 编辑

This appears to be a bug that happens with Android OS on occasion. 这似乎是Android OS偶尔发生的错误。 Check out this link, it appears it has been addressed already. 看看这个链接,它似乎已经解决了。 "Native typeface cannot be made" only for some people “只能为某些人制作”原生字体“

It may be that you are referring to a font file that does not exist. 可能是您指的是不存在的字体文件。 Your directory listing does not show fonts/Tahoma.ttf , which is what your code is referring to. 您的目录列表中没有显示fonts/Tahoma.ttf ,这是您的代码所指的内容。

Beyond that, it may be that Android simply does not like your font file for some reason. 除此之外,Android可能因某种原因而不喜欢您的字体文件。 Try a different font file. 尝试使用其他字体文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM