gpt4 book ai didi

java - Android使用本地CSS显示html字符串

转载 作者:行者123 更新时间:2023-11-29 22:26:26 25 4
gpt4 key购买 nike

好的,我要做的是:

我有一个在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com