android中webview加载html,用本地的css渲染页面如何做

2024-11-06 09:07:54
推荐回答(1个)
回答1:

android中webview加载html用本地css渲染的做法:
1:将资源拷贝到assets目录
2:给需要注入的资源文件在url上做一个标志,当然,你也可以不做,只要你在安卓端可以判断出来即可。




3:拦截将要注入的文件,读取本地文件即可。

webview.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
//System.out.println("WebResourceResponse::"+url);
if(url.contains("[inject]")){
String localPath = url.replaceFirst("^http.*inject\\]","");
System.out.println("LocalPath::"+localPath);
try {
InputStream is = getApplicationContext().getAssets().open(localPath);
return new WebResourceResponse("text/javascript", "UTF-8", is);
} catch (Exception e) {
e.printStackTrace();
return super.shouldInterceptRequest(view, url);
}
} else {
return super.shouldInterceptRequest(view, url);
}
}
});