Android webview有两种加载数据的方法
public void loadData (String data, String mimeType, String encoding)
Run Code Online (Sandbox Code Playgroud)
请注意,JavaScript的相同原始策略意味着在使用此方法加载的页面中运行的脚本将无法访问使用"数据"以外的任何方案加载的内容,包括"http(s)".要避免此限制,请将loadDataWithBaseURL()与适当的基本URL一起使用.
和
public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
Run Code Online (Sandbox Code Playgroud)
请注意,仅当baseUrl指定除"http","https","ftp","ftps","about"或"javascript"以外的方案时,以这种方式指定的内容才能访问本地设备文件(通过"文件"方案URL) ".
我不知道这两句话意味着什么,何时在两者之间进行选择?
提前致谢
我使用以下代码加载电子书的html内容,其中templateString包含连接到主文件中的样式表和图像的html内容。
String itemURL = "file://" + itemPath;
testWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", "about:blank");
Run Code Online (Sandbox Code Playgroud)
我面临的问题是锚链接根本没有响应。
我注意到itemURL为null还是使用loadData而不是loadDataWithBaseURL,这些链接都可以工作,但是我松开了图像的连接以及通过itemURL连接的样式。
请注意,Webview可见性始终设置为可见。
这是为Webview初始化的onTouch方法:
this.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
System.out.println("getExtra: "+hr.getExtra());
// getExtra always gives null when webview loaded with loadDataWithBaseURL while it should give the url of the link pressed when the user touches a link
return false;
}
});
Run Code Online (Sandbox Code Playgroud)