android webview 怎样加载本地css
第一步:将资源拷贝到assets目录
第二步:给需要注入的资源文件在url上做一个标志,当然,你也可以不做,只要你在安卓端可以判断出来即可。
<link rel="stylesheet" href="[inject]public/css/bootstrap.min.css" type="text/css" media="screen" charset="utf-8">
<link rel="stylesheet" href="[inject]public/css/bootstrap-theme.min.css" type="text/css" media="screen" charset="utf-8">
<script type="text/javascript" src="[inject]public/js/zipto/1.1.6/zepto.min.js"></script>
<script type="text/javascript" src="[inject]public/js/modules/md5.min.js"></script>
第三步:拦截将要注入的文件,读取本地文件即可。
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);
}
}
});
标签:android,webview,css