2 znl 12 znl_12 于 2013.04.28 17:56 提问

如何在 HTML 中使用自定义的字体?

我实时生成HTML 页面,我想把字体文件放进assets 文件夹中 ,"MyriadProRegular.otf"。

String mStyle = "<style type=\"text/css\"> body { font-size: 100%; font-family: 'MyriadProRegular'; src: url('file:///android_asset/MyriadProRegular.otf'); }  </style>";
String mHTML = "<html><head> " + mStyle + " </head><body>"
    + " Hello World " + "</body></html>";

加载方法:

WebView mWebview = new WebView(this);
mWebview.loadDataWithBaseURL(null, mHTML, "text/html","utf-8", null);

HTML没有加载上字体
我用这个代码试了试:

body { font-size: 100%; font-family: 'MyriadProRegular'; src:url('MyriadProRegular.otf'); } 

和:

mWebview.loadDataWithBaseURL("file:///android_asset/", mHTML, "text/html","utf-8", null);

但是还是没有加载上去,我该怎么改呢?
log 信息:

04-26 11:39:34.724: E/Trace(2090): error opening trace file: No such file or directory (2)
04-26 11:39:35.923: D/dalvikvm(2090): GC_CONCURRENT freed 53K, 3% free 8410K/8583K, paused 40ms+28ms, total 216ms
04-26 11:39:36.253: I/Choreographer(2090): Skipped 56 frames!  The application may be doing too much work on its main thread.
04-26 11:39:36.293: D/gralloc_goldfish(2090): Emulator without GPU emulation detected.
04-26 11:39:36.794: I/Choreographer(2090): Skipped 80 frames!  The application may be doing too much work on its main thread.

PS: 我不知道这个 log 信息与问题有关系没,特别是 " error opening trace file:" 这句话,当我每次运行程序的时候都出现。

1个回答

franzhong
franzhong   2013.04.28 18:28
已采纳
@font-face {
   font-family: 'myface';
   src: url('file:///android_asset/fonts/myfont.ttf'); 
} 

body { 
   font-family: 'myface', serif;

试一下这样调用,能不能运行不太清楚 --!
那个错误提示不关紧要,不是版本不统一问题就是xml文件问题
不影响运行就行

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!