【发布时间】:2019-12-01 13:29:03
【问题描述】:
我使用以下代码加载电子书的 html 内容,其中 templateString 包含连接到主文件中的样式表和图像的 html 内容。
String itemURL = "file://" + itemPath;
testWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", "about:blank");
我面临的问题是锚链接根本没有响应。
我注意到如果 itemURL 为空,或者如果我使用 loadData 而不是 loadDataWithBaseURL,链接可以工作,但我失去了图像的连接以及通过 itemURL 连接的样式。
请注意,webview 可见性始终设置为可见。 添加我在 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;
}
});
如果需要更多代码,我可以分享。
【问题讨论】:
标签: android webview hyperlink loaddata