在Android中将Res/font中的自定义字体与WebView一起使用 [英] Use custom font from res/font with WebView in Android

查看:347
本文介绍了在Android中将Res/font中的自定义字体与WebView一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改加载到WebView中的html字符串的字体,类似于此问题中所述:

I want to change font face of html string loaded into WebView similarly as mentioned in this question:

如何在Android中更改Webview的字体? /a>

How to change font face of Webview in Android?

区别在于,我没有使用旧的方法将字体文件存储在Assets文件夹中,而是将它们存储在res/font中,如"XML字体" android字体支持文档中所述:

The difference is that I am not using old approach where you store you font files in assets folder, but I store them in res/font as described in "Fonts in XML" android font support documentation:

https://developer. android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

现在,我显然不能使用:

Now, I obviously can't use:

file:///android_asset/fonts/my_font.otf

我尝试过:

file:///android_res/font/my_font.otf

以及在res/font文件夹中描述我的字体路径的许多其他方式,但是它们都不起作用.

and many other ways of describing path to my font inside of res/font folder, but none of them work.

如果我的字体存储在res/font文件夹中,如何为加载html字符串的WebView使用自定义字体系列?

How to use custom font family for a WebView that loads html string if my font is stored in res/font folder ?

//

我当前无法执行的实现是:

My current implementation that is not working is:

@BindingAdapter("loadData")
public static void loadData(WebView webView, String htmlData) {
    webView.loadDataWithBaseURL(null, htmlData, "text/html", "utf-8", null);
}

@BindingAdapter({"loadData", "fontFamily"})
public static void loadData(WebView webView, String htmlData, @FontRes int fontFamilyId) {
    TypedValue value = new TypedValue();
    ApplicationActivity.getSharedApplication().getResources().getValue(fontFamilyId, value, true);
    String fontPath = value.string.toString();
    File fontFile = new File(fontPath);

    String prefix = "<html>\n"
            +"\t<head>\n"
            +"\t\t<style type=\"text/css\">\n"
            +"\t\t\t@font-face {\n"
            +"\t\t\t\tfont-family: 'CustomFont';\n"
            +"\t\t\t\tsrc: url(\"file:///android_res/font/"+fontFile.getName()+"\")\n"
            +"\t\t\t}\n"
            +"\t\t\tbody {\n"
            +"\t\t\t\tfont-family: 'CustomFont';\n"
            +"\t\t\t}\n"
            +"\t\t</style>\n"
            +"\t</head>\n"
            +"\t<body>\n";
    String postfix = "\t</body>\n</html>";

    loadData(webView, prefix + htmlData + postfix);
}

推荐答案

只需尝试一下,它的工作原理与从资源中加载字体类似,您只需要更改基本URL即可指向资源.

Just tried and this works similarly to loading fonts from assets, you just need to change the base url to point to resources instead.

示例HTML

<html>
<head>
    <style>
        @font-face {
            font-family: 'CustomFont';
            src: url('font/CustomFont.ttf');
        }
        #font {
            font-family: 'CustomFont';
        }
    </style>
</head>
<body>
    <p>No Font</p>
    <br />
    <p id="font">Font</p>
</body>

使用file://android_res/作为基本网址(第一个参数),使用Webview#loadDataWithBaseURL(String, String, String, String, String)将此HTML加载到Web视图中.

load this HTML into the webview using Webview#loadDataWithBaseURL(String, String, String, String, String), using file://android_res/ as the base url (first param)

示例:

webview.loadDataWithBaseURL("file:///android_res/", html, "text/html", "utf-8", null)

如果在发行版本上使用proguard,则需要添加其他规则,以防止在ProGuard过程中重命名R类,否则WebView将无法找到字体资源.可以在此帖子中找到详细信息

If you're using proguard on your release builds you'll need to add extra rules to prevent the R class being renamed during the ProGuard process otherwise the WebView won't be able to find the font resource. Details can be found at this post

这篇关于在Android中将Res/font中的自定义字体与WebView一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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