静的htmlファイル「myhtml.html」がassets/html /ディレクトリに保存されており、このhtmlはカスタムフォント(assets / fonts /の下に保存)を次のようにロードします。
<style type="text/css">
@font-face {
font-family: 'MyFont';
src: url('file:///android_asset/fonts/myfont.ttf');
}
</style>
次のコードを使用してこのhtmlをロードしています。
String html = getHtml(); // This method loads the myhtml.html from asset. This loads properly.
WebView webView = (WebView) findViewById(R.id.webbox);
WebSettings webSettings = webView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setFixedFontFamily("fonts/myfont.ttf");
webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);
このコードは、Android 4.2(API 17)を除くすべてのAndroidバージョンで正常に機能します。
Android 4.2では、HTMLは読み込まれますが、カスタムフォントは読み込まれません。