Android WebView 部分 HTML 渲染问题
Android WebView partial HTML rendering problem
我的HTML很简单,应该是这样渲染的
然而 android webview 丢失了一些内容并呈现为这样
导致问题的行是这个
<a name="_ftnref1" href="#_ftn1">[1]</a>
如何调整 HTML 或 webview 以便正确呈现所有内容?
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>
尝试将此发送到您的 activity_main.xml
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
奇怪的是,我通过从文件而不是从内存加载 html 内容解决了我的问题。为什么它以这种方式工作而以另一种方式不工作我不知道。
// HTML will be rendered correctly
val file = File(filesDir, "temp.html")
file.writeText(html, Charset.forName("UTF-8"))
val webView = findViewById<WebView>(R.id.web_view)
webView.settings.allowFileAccess = true
webView.loadUrl("file://" + file.absolutePath)
// HTML will be rendered incorrectly
// webView.loadData(html, "text/html", "UTF-8")
我的HTML很简单,应该是这样渲染的
然而 android webview 丢失了一些内容并呈现为这样
导致问题的行是这个
<a name="_ftnref1" href="#_ftn1">[1]</a>
如何调整 HTML 或 webview 以便正确呈现所有内容?
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>
尝试将此发送到您的 activity_main.xml
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
奇怪的是,我通过从文件而不是从内存加载 html 内容解决了我的问题。为什么它以这种方式工作而以另一种方式不工作我不知道。
// HTML will be rendered correctly
val file = File(filesDir, "temp.html")
file.writeText(html, Charset.forName("UTF-8"))
val webView = findViewById<WebView>(R.id.web_view)
webView.settings.allowFileAccess = true
webView.loadUrl("file://" + file.absolutePath)
// HTML will be rendered incorrectly
// webView.loadData(html, "text/html", "UTF-8")