在Android中使用来自res/font的自定义字体与WebView

1da*_*on1 11 fonts android webview font-face custom-font

我想改变加载到WebView中的html字符串的字体,就像这个问题中提到的那样:

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

不同之处在于我没有使用旧方法将字体文件存储在assets文件夹中,但我将它们存储在res/font中,如"字体在XML中"android字体支持文档中所述:

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

现在,我显然不能使用:

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

我试过了:

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

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

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

//编辑:

我目前的实施不起作用是:

@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);
}
Run Code Online (Sandbox Code Playgroud)

Rez*_*eza 10

我设法从res/font目录加载了我的本地字体。在我的示例中,我想加载斜体 opensan。

我的设置如下:

  1. assets目录下的css文件
  2. 使用它来设置字体
    @font-face {
      font-family: 'opensans';
      src: url("file:///android_res/font/opensans_italic.ttf")
    }

    body {
      font-family: 'opensans';
      font-weight: 400;
    }
Run Code Online (Sandbox Code Playgroud)
  1. 使用 加载 WebView loadDataWithBaseUrl,例如:

    webView.loadDataWithBaseURL(null, yourHtmlContent, "text/html", "utf-8", null)

    但是我在 html 内容的顶部有这个: "<link rel='stylesheet' type='text/css' href='file:///android_asset/mycss.css' />”

    我希望它也适用于您的情况。

编辑:

将此添加到您的 proguard 配置中以使其在发布模式下工作。

-keep class com.your.package.name.R$font { *; }


hib*_*bob 9

刚试过,这与从资源加载字体的工作方式类似,您只需要更改基本URL以指向资源.

示例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>
Run Code Online (Sandbox Code Playgroud)

将此HTML加载到webview中Webview#loadDataWithBaseURL(String, String, String, String, String),使用,file://android_res/作为基本URL(第一个参数)

例:

webview.loadDataWithBaseURL("file:///android_res/", html, "text/html", "utf-8", null)
Run Code Online (Sandbox Code Playgroud)

编辑:

如果您在发布版本中使用proguard,则需要添加额外的规则以防止R在ProGuard过程中重命名该类,否则WebView将无法找到字体资源.详细信息可以在这篇文章中找到