首页 > 解决方案 > 在android中以自定义字体显示webview的html内容

问题描述

我在 web 视图中显示了下面的 HTML 内容

<p><a href="https://www.google.com"><cite>Here</cite></a></p>

如何应用.ttf file我在资产文件夹中的自定义字体()

标签: android

解决方案


尝试这样做:

WebView webview = findViewById(R.id.webView1);
String start = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"YOUR CUSTOM FONT ASSET HERE")}body {font-family: MyFont;font-size: medium;text-align: justify;}</style></head><body>";
String end = "</body></html>";
String mHtmlString = start + "<p><a href="https://www.google.com"><cite>Here</cite></a></p>" + end;
webview.loadDataWithBaseURL(null,mHtmlString, "text/html", "UTF-8", null);

推荐阅读