Android Webview 锚链接(跳转链接)不起作用

发布于 2024-09-05 05:46:43 字数 755 浏览 4 评论 0原文

我的 Android 应用程序中有一个 WebView,它使用 loadDataWithBaseURL() 方法加载 HTML 字符串。问题是本地锚链接 (...) 无法正常工作。单击链接时,它会突出显示,但不会滚动到相应的锚点。

如果我使用 WebView 的 loadUrl() 方法加载包含锚链接的页面,这也不起作用。但是,如果我在浏览器中加载相同的 URL,锚链接就会起作用。

是否需要进行任何特殊处理才能使它们适用于 WebView?

我正在使用 API v4 (1.6)。

代码不多,以下是我一直在使用的一些测试代码的相关部分:

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", "");

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.

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.

Is there any special handling required to get these to work for a WebView?

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", "");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浮萍、无处依 2024-09-12 05:46:43

看起来问题是我在 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.

莳間冲淡了誓言ζ 2024-09-12 05:46:43

Android Webview 锚链接(跳转链接)不起作用

当 WebView 位于 ScrollView(*) 内部时,通过 URL 的 #LINK 扩展启动的 WebView 锚链接或跳转链接将不起作用。

不过,对我和显然其他人来说,问题是 #LINK 在通过 href 中的触摸启动时确实可以工作,但在通过 URL 启动时会被忽略。其他症状包括仅在会话中第一次导航到链接或导航到 html 文件的底部。

解决方案是在短暂延迟后加载网址。

这是一个示例:

我的html保存在资产中:res/assets/help.html

带有这样的锚点:

<a name="helplinkcontacts"/>

并像这样加载:

final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
helpTextView.loadUrl(baseUrl); // Ignores Anchor!!

我添加了计时器如下:

final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        helpTextView.loadUrl(baseUrl);
    }
}, 400);

注意:较短的延迟,例如 100ms 未能导航到链接。

(*) 事实证明,我们很多人都在 ScrollView 中拥有 WebView,因为我们一开始使用 TextView 渲染 Spannable 文本,它既支持一些 HTML,又需要 ScrollView。无论如何,将 TextView 转换为 WebView 后立即删除 ScrollView。

Android Webview Anchor Link (Jump link) Not Working

True, WebView Anchor Links, or Jump Links initiated through the #LINK extension to the URL will not work when the WebView is inside of a ScrollView(*).

Still, the problem for me and apparently others is that the #LINK does work when launched from a touch in an href, but is ignored when launched via the URL. Other symptoms include navigating to the link only on the first time in a session or navigating to the bottom of the html file.

The Solution is to load the url after a short delay.

Here is an example:

My html is saved in assets: res/assets/help.html

With anchors like this:

<a name="helplinkcontacts"/>

And loaded like this:

final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
helpTextView.loadUrl(baseUrl); // Ignores Anchor!!

I added the timer like this:

final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        helpTextView.loadUrl(baseUrl);
    }
}, 400);

Note: Shorter delays, such as 100ms failed to navigate to the link.

(*) It turns out that so many of us have our WebViews inside of ScrollViews because we started out with a TextView rendering Spannable text which both supports some HTML and requires a ScrollView. Anyways, remove the ScrollView as soon as you convert your TextView into a WebView.

青萝楚歌 2024-09-12 05:46:43

我的解决方案是,检查这个答案

public class MainActivity extends Activity { 
    WebView myWebView; 
    public static boolean flag = false; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        myWebView = new WebView(this); 
        myWebView.getSettings().setJavaScriptEnabled(true); 
        myWebView.loadUrl("file:///android_asset/chapters.html"); 
        setContentView(myWebView); 
        myWebView.setWebViewClient(new WebViewClient() { 
            public void onPageFinished(WebView view, String url) { 
                if (url.contains("#") && flag == false) { 
                    myWebView.loadUrl(url); 
                    flag = true; 
                } else { 
                    flag = false; 
                } 
            } 

        }); 
    } 
} 

My Solution is , Check this Answer

public class MainActivity extends Activity { 
    WebView myWebView; 
    public static boolean flag = false; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        myWebView = new WebView(this); 
        myWebView.getSettings().setJavaScriptEnabled(true); 
        myWebView.loadUrl("file:///android_asset/chapters.html"); 
        setContentView(myWebView); 
        myWebView.setWebViewClient(new WebViewClient() { 
            public void onPageFinished(WebView view, String url) { 
                if (url.contains("#") && flag == false) { 
                    myWebView.loadUrl(url); 
                    flag = true; 
                } else { 
                    flag = false; 
                } 
            } 

        }); 
    } 
} 
美煞众生 2024-09-12 05:46:43

我有类似的问题。没有任何内容会跳转到 html 中的锚标记。我的 WebView 没有在 ScrollView 中。相反,问题是我传递给 loadDataWithBaseURL 的基本 url 中没有冒号(':')。我相信 baseUrl 需要有一些文本,然后是冒号,然后是更多文本,例如“app:htmlPage24”。

因此,这是对我的 WebView 的初始调用,只是为了加载字符串 HTML_24 中的数据:

wv.loadDataWithBaseURL("app:htmlPage24", HTML_24, "text/html", "utf-8", null);

然后我有一个列表,根据您点击的列表项,跳转到屏幕上的各个部分:

sectionsLV.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
    {
        wv.loadUrl("app:htmlPage24#section" + arg2);
    }
});

HTML_24 类似于:

<html>
...
<a name="section1"/>

...
<a name="section2"/>

...
<a name="section3"/>

...
</html>

I had a similar problem. Nothing would jump to anchor tags in the html. I didn't have my WebView within a ScrollView. Instead the problem was the base url I passed into loadDataWithBaseURL did not have a colon (':') in it. I believe the baseUrl needs to have some text, then a colon, then some more text, for example "app:htmlPage24".

So here's the initial call to my WebView, just to load the data in the string HTML_24:

wv.loadDataWithBaseURL("app:htmlPage24", HTML_24, "text/html", "utf-8", null);

Then I have a list that jumps to sections on the screen, depending on the list item you tap:

sectionsLV.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
    {
        wv.loadUrl("app:htmlPage24#section" + arg2);
    }
});

HTML_24 is something like:

<html>
...
<a name="section1"/>

...
<a name="section2"/>

...
<a name="section3"/>

...
</html>
两个我 2024-09-12 05:46:43

我也面临着同样的问题。锚链接跳转到任意位置。确保加载时不要隐藏 webview

使用Invisible而不是gone

此更改解决了我的问题。

I was also facing the same issue. Anchor link was jumping to arbitrary position. Make sure not to hide webview when loading.

Use Invisible instead of gone.

This change fixed my issue.

二手情话 2024-09-12 05:46:43
try this

String myTemplate = "<a href=\"#link\">LINK!</a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!";

myWebView.loadDataWithBaseURL(null, myTemplate, "text/html", "utf-8", null);

“测试!”这个词必须在屏幕之外才能看到它的工作原理。

try this

String myTemplate = "<a href=\"#link\">LINK!</a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!";

myWebView.loadDataWithBaseURL(null, myTemplate, "text/html", "utf-8", null);

the word "Testing!" must be outside of the screen to see it works.

骄兵必败 2024-09-12 05:46:43

Android 4.0 中的 WebView 无法打开包含链接的 URL。
例如
“file:///android_asset/help.html#helplinkcontacts”

这是我如何解决它

WebView wv = (WebView) nagDialog.findViewById(R.id.wv);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new MyWebViewClient(link));
wv.loadUrl("file:///android_asset/help.html");

并定义自定义 WebViewClient 类

class MyWebViewClient extends WebViewClient {
    private String link;

    public MyWebViewClient(String link) {
        this.link = link;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (!"".equals(link) && link != null)
            view.loadUrl("javascript:location.hash = '#" + link + "';");
    }
}

WebView in Android 4.0 fails to open URLs with links in them.
e.g.
"file:///android_asset/help.html#helplinkcontacts"

Here is how I got around it

WebView wv = (WebView) nagDialog.findViewById(R.id.wv);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new MyWebViewClient(link));
wv.loadUrl("file:///android_asset/help.html");

And define the custom WebViewClient class

class MyWebViewClient extends WebViewClient {
    private String link;

    public MyWebViewClient(String link) {
        this.link = link;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (!"".equals(link) && link != null)
            view.loadUrl("javascript:location.hash = '#" + link + "';");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文