Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

Android WebView partial HTML rendering problem

My HTML is very simple and it should be rendered like this

enter image description here

However android webview loses some of the content and renders it like this

enter image description here

The line that is causing the problem is this <a name="_ftnref1" href="#_ftn1">[1]</a>

How do I tweak HTML or the webview so that everything is rendered correctly?

MainActivity.kt

import android.os.Bundle
import android.webkit.WebView

class MainActivity : AppCompatActivity() {

    val html = """
        <!DOCTYPE html>
        <html>
            <head></head>
            <body>
                <ul>
                    <li>content above link</li>
                    <li>The link is on this line 
                        <a name="_ftnref1" href="#_ftn1">[1]</a>
                    </li>
                    <li>content below link</li>
                </ul>
                <p>
                    <a name="_ftn1" href="#_ftnref1">[1]</a> Link landing
                </p>
            </body>
        </html>
    """.trimIndent()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<WebView>(R.id.web_view).loadData(html, "text/html", "UTF-8")
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to at this to your activity_main.xml

<supports-screens android:smallScreens="true"
      android:normalScreens="true" 
      android:largeScreens="true"
  android:anyDensity="true" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...