Android Webview Anchor Link (Jump link) not working

前端 未结 7 1218
后悔当初
后悔当初 2020-12-01 06:53

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 (

相关标签:
7条回答
  • 2020-12-01 07:18

    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>
    
    0 讨论(0)
提交回复
热议问题