在Android WebView中使用Font-face

时间:2016-06-03 16:45:26

标签: android html css webview fonts

我想在我的WebView中插入font-face,以便在不同的网站上显示一些特殊字体。

例如在维基百科中,我希望我使用一些代码来更改html中的字体,当这样做时,我显示页面并且字体不是像html代码中的Century Gothic。当我尝试使用谷歌浏览器时,我也不会工作。当我尝试:

webView.loadDataWithBaseURL(...) 

如果我加上标题就可以了:

"<link rel='stylesheet' type='text/css' href='file:///android_asset/style.css'/>"

在我的style.css中:

@font-face {
    font-family: Andika;
    src: url('fonts/Andika.ttf');
}
@font-face {
    font-family: Calibri;
    src: url('fonts/Calibri.ttf');
}
@font-face {
    font-family: Century Gothic;
    src: url('fonts/CenturyGothic.ttf');
}
@font-face {
    font-family: Comic Sans MS;
    src: url('fonts/ComicSansMS.ttf');
}

@font-face {
    font-family: Lexia;
    src: url('fonts/Lexia.ttf');
}
@font-face {
    font-family: Lucida Sans Unicode;
    src: url('fonts/LucidaSansUnicode.ttf');
}
@font-face {
    font-family: Tahoma;
    src: url('fonts/Tahoma.ttf');
}
@font-face {
    font-family: Tiresias;
    src: url('fonts/Tiresias.ttf');
}
@font-face {
    font-family: Trebuchet MS;
    src: url('fonts/TrebuchetMS.ttf');
}
@font-face {
    font-family: Verdana;
    src: url('fonts/Verdana.ttf');
}

但它取决于本地数据。

我想用维基百科等外部网页制作。

当我在维基百科html页面上注入链接时,我有以下错误消息:

Not allowed to load local resource: file:///android_asset/

如果我直接在样式字段中放置@font-face,我会遇到同样的错误。

SomeOne有解决这个错误的想法吗?

请!

谢谢

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

  1. 使用任何HTTP客户端获取维基百科页面的HTML
  2. 使用JSoup解析它并在其中插入<link...标记
  3. 使用loadDataWithBaseURL
  4. 中的WebView加载