好的,我要做的是:
我有一个在 WebView 中显示的 html 字符串,如下所示:
webView.loadDataWithBaseURL("",htmlText, "text/html","utf-8", null);
我还有一个本地 css 文件,我想将其应用到 webView 中的那个 html 字符串。
我查了一会儿,但似乎没有解决方案。
应该有一种方法可以将 css 文件保存在 assets 文件夹中并从某个地方调用它,但我不知道如何操作。
和建议?
最佳答案
你不能在 Assets 文件夹中写任何东西。
既然你同时拥有 html 和 css,我想你可以像这样将 css 文本插入到 head 标签中
int pos = htmlText.toLowerCase().indexOf("</head>");
if(pos != -1) {
String newHtmlText = htmlText.substring(0, pos) + String.format("<style>%s</style>", cssText) + htmlText.substring(pos + "</head>".length());
webView.loadDataWithBaseURL("",newHtmlText , "text/html","utf-8", null);
}
免责声明:未经测试:))
关于java - Android使用本地CSS显示html字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5806850/