资产字体未在Android的web视图加载 [英] Asset font is not loaded in android webView

查看:182
本文介绍了资产字体未在Android的web视图加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发它加载HTML文件到网页视图的机器人活动。
然而,这acitivty不是在某些手机上加载的字体如HTC Desire的或索尼Xperia Z,使用4.4或4.1的机器人。
我想知道如果我错过了一些东西,或者是仅依赖于手机当中我测试我的应用程序。

I'm developing an android activity which is loading an HTML file into a webview.
However this acitivty is not loading fonts in some phones e.g. HTC Desire or Sony Xperia Z with 4.4 or 4.1 androids.
I want to know if i have missed something or it is only depends on phone which i'm testing my app.

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

下面是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>

字体位于资产/字体/像这样

fonts are located in assets/fonts/ like this

推荐答案


  • 所有首先,字体路径应该是相对于你的HTML / CSS文件。

  • First of all, fonts path should be relative to your HTML/CSS file.
  • 所以,与其这样:

    @font-face { font-family: 'Persian'; src: url('file:///android_asset/fonts/b_yekan.ttf');
    

    使用这样的:

    @font-face { font-family: 'Persian'; src: url('fonts/b_yekan.ttf');
    


    • ,你必须确保你对测试的目标实际上是支持阿拉伯文字。

      • Second, you must make sure that the target you are testing against actually supports Arabic script.
      • 通过了,下面我提供一个工作的例子。

        With that, below I am providing a working example.

        资产/ 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>
        

        BHOMA.TTF BLOTUS.TTF BLOTUSBD.TTF 来自的 bornaray.com 并存储在资产/字体/ 文件夹中。

        BHOMA.TTF, BLOTUS.TTF and BLOTUSBD.TTF are downloaded from bornaray.com and stored in assets/fonts/ folder.

        loadToWebView()的,它是一个片段,因此, getActivity()的一部分。getAssets()

        loadToWebView(), it is part of a Fragment, hence 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();
            }
        }
        

        而结果的:

        希望这有助于。

        这篇关于资产字体未在Android的web视图加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆