Android Webview 锚链接(跳转链接)不起作用 [英] Android Webview Anchor Link (Jump link) not working
问题描述
我的 Android 应用中有一个 WebView,它使用 loadDataWithBaseURL() 方法加载 HTML 字符串.问题是本地锚链接 (...) 无法正常工作.当链接被点击时,它会突出显示,但不会滚动到相应的锚点.
I have a WebView in my Android App that is loading an HTML string using the loadDataWithBaseURL() method. The problem is that local anchor links (<a href="#link">
...) are not working correctly. When the link is clicked, it becomes highlighted, but does not scroll to the corresponding anchor.
如果我使用 WebView 的 loadUrl() 方法加载包含锚链接的页面,这也不起作用.但是,如果我在浏览器中加载相同的 URL,则锚链接确实有效.
This also does not work if I use the WebView's loadUrl() method to load a page that contains anchor links. However, if I load the same URL in the browser, the anchor links do work.
是否需要进行任何特殊处理才能使它们适用于 WebView?
Is there any special handling required to get these to work for a WebView?
我使用的是 API v4 (1.6).
I am using API v4 (1.6).
代码不多,以下是我一直在使用的一些测试代码的相关部分:
There isn't much to the code, here are the relevant parts of some test code I've been working with:
WebView detailBody = (WebView) findViewById(R.id.article_detail_body);
String s = "<a href="#link">LINK!</a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name="link"></a>Testing!";
detailBody.loadDataWithBaseURL(API.HomeURL(this), s, "text/html", "utf-8", "");
推荐答案
看起来问题是我在 ScrollView 中有一个 WebView.像这样配置时,WebView 无法滚动到锚链接.重构我的布局以消除 ScrollView 后,锚链接正常工作.
It looks like the problem is that I had a WebView within a ScrollView. The WebView isn't able to scroll to an anchor link when configured like this. After refactoring my layout to eliminate the ScrollView, the anchor links work correctly.
这篇关于Android Webview 锚链接(跳转链接)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!