android webView中未加载资源字体

VSB*_*VSB 4 css fonts android webview

我正在开发一个将HTML文件加载到webview中的android活动.
然而,这种活动并不是在某些手机中加载字体,例如HTC Desire或Sony Xperia Z的4.4或4.1机器人.
我想知道我是否遗漏了某些东西,或者仅仅取决于我正在测试我的应用程序的手机.

private void loadToWebView(String s) {
    try {
        pageWebView.loadDataWithBaseURL("file:///android_asset/", s,
                "text/html", "utf-8", null);
        configureWebView(pageWebView);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是html标题:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style type="text/css">
@font-face { font-family: 'Persian'; src: url('file:///android_asset/fonts/b_yekan.ttf'); }
@font-face { font-family: 'Persian2'; src: url('file:///android_asset/fonts/b_homa.ttf'); }
@font-face { font-family: 'PersianTitle'; src: url('file:///android_asset/fonts/b_titr.ttf');}
body {font-family: 'Persian';}
h1 {font-family: 'PersianTitle';}
h2 {font-family: 'Persian2';}
</style>
Run Code Online (Sandbox Code Playgroud)

字体位于assets/fonts /中
在此输入图像描述

ozb*_*bek 21

  • 首先,字体路径应该与您的HTML/CSS文件相关.

所以,而不是这个:

@font-face { font-family: 'Persian'; src: url('file:///android_asset/fonts/b_yekan.ttf'); }
Run Code Online (Sandbox Code Playgroud)

使用这样的东西:

@font-face { font-family: 'Persian'; src: url('fonts/b_yekan.ttf'); }
Run Code Online (Sandbox Code Playgroud)
  • 其次,您必须确保您要测试的目标实际上支持阿拉伯语脚本.

有了这个,下面我提供一个工作的例子.


assets/about_us.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style type="text/css">
    @font-face { font-family: 'B Homa'; src: url('fonts/BHOMA.TTF');}
    @font-face { font-family: 'B Lotus'; src: url('fonts/BLOTUS.TTF');}
    @font-face { font-family: 'B Lotus Bold'; src: url('fonts/BLOTUSBD.TTF');}
</style>
</head>
<body>
    <p style="font-family:'B Homa';font-size:20px;">B Homa: ????</p>
    <p style="font-family:'B Lotus';font-size:20px;">B Lotus: ????</p>
    <p style="font-family:'B Lotus Bold';font-size:20px;">B Lotus Bold: ????</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

BHOMA.TTF,BLOTUS.TTFBLOTUSBD.TTFbornaray.com下载并存储在assets/fonts/文件夹中.

loadToWebView(),它是a的一部分Fragment,因此getActivity().getAssets():

private void loadToWebView() {
    AssetManager assetManager = getActivity().getAssets();
    try {
        InputStream input = assetManager.open("about_us.html");
        byte[] buffer = new byte[input.available()];
        input.read(buffer);
        input.close();
        mWebView.loadDataWithBaseURL("file:///android_asset/",
                new String(buffer), "text/html", "UTF-8", null);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

截图

希望这可以帮助.