无法在 Android WebView 中更改自定义 res/font

Unable to change custom res/font in Android WebView

我正在使用自定义字体从 strings.xml 中的 android WebView 中加载一个字符串,但无法在 UI 上看到更改。相反,我在控制台中收到警告:

[INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_res/font/regular.ttf"

strings.xml

 <string name="app_sample_text">"<![CDATA[
 <!DOCTYPE html><html><head>
 <style type="text/css">
 @font-face {
 font-family: regular;
 src: url("file:///android_res/font/regular.ttf")
 }
 body {
 font-family: regular;
 font-size: medium;
 text-align: justify;
 }
 </style>
 </head><body><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
"</string>

在 WebView 中,加载为:

   val text = getString(R.string.app_sample_text)
   binding.webView.loadData(text, MIME_TYPE, ENCODING)

.ttf 文件的路径是 res/font/regular.ttf

但是,我仍然看到 WebView 的默认字体。如果我做错了请告诉我。

使用 WebViewAssetLoader 找到问题的解决方案:

val assetLoader = activity?.let { WebViewAssetLoader.ResourcesPathHandler(it) }?.let {
        WebViewAssetLoader.Builder()
            .addPathHandler(WEBVIEW_RES_PATH_HANDLER, it)
            .build()
    }

    binding.webView.webViewClient = object : WebViewClient() {
        override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest
        ): WebResourceResponse? {
            return assetLoader?.shouldInterceptRequest(request.url)
        }
    }
    binding.webView.loadDataWithBaseURL(WEBVIEW_BASE_URL, text, MIME_TYPE, ENCODING, null)
}

companion object {
    const val WEBVIEW_RES_PATH_HANDLER = "/res/"
    const val WEBVIEW_BASE_URL = "https://appassets.androidplatform.net"
}