我使用以下代码加载电子书的 html 内容,其中 templateString 包含连接到主文件中的样式表和图像的 html 内容。
String itemURL = "file://" + itemPath;
testWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", "about:blank");
我面临的问题是锚链接根本没有响应。
我注意到,如果 itemURL 为 null 或者如果我使用 loadData 而不是 loadDataWithBaseURL,则链接可以工作,但我会失去通过 itemURL 连接的图像和样式的连接。
请注意,网络视图可见性始终设置为可见。
添加我已将以下功能添加到 webview
this.getSettings().setJavaScriptEnabled(true);
this.requestFocusFromTouch();
this.setVerticalScrollBarEnabled(false);
this.setHorizontalScrollBarEnabled(false);
this.getSettings().setSupportZoom(true);
this.getSettings().setBuiltInZoomControls(true);
this.getSettings().setDisplayZoomControls(false);
this.getSettings().setAllowContentAccess(true);
this.getSettings().setAllowFileAccess(true);
this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.getSettings().setAllowFileAccessFromFileURLs(true);
this.getSettings().setAllowUniversalAccessFromFileURLs(true);
这是为 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;
}
});
如果需要更多代码,我可以分享。