android webView中的自定义字体

时间:2015-05-14 18:07:10

标签: android fonts webview

我在webview上使用了这个字体

            String data = "<!DOCTYPE html>";
            data +="<head>\n" +
                    "<style type=\"text/css\">\n" +
                    "@font-face {\n" +
                    "    font-family: MyFont;\n" +
                    "    src: url(\'file:///android_asset/fonts/MyFont.otf\')\n" +
                    "}\n" +
                    "body {\n" +
                    "    font-family: MyFont;\n" +
                    "    font-size: medium;\n" +
                    "    text-align: justify;\n" +
                    "}\n" +
                    "</style>\n" +
                    "</head>";
            data += "<body dir=\"rtl\" style=\"text-align:justify\">";
            data += "my text";
            data +="</body></html>";
            // args: data, mimeType, encoding
            web1.loadData(`data, "text/html", null);

但它不适合我的字体。非常感谢

1 个答案:

答案 0 :(得分:0)

您需要指定file:///android_asset作为数据的基本网址:URI:

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