我正在尝试使用 Android WebView 打开
odoo
网站。但我总是得到一个空白页。
没有错误或警告,其他页面打开没有问题。但似乎Odoo页面的WebView中有特定的设置。
我为 iOS 创建了相同的应用程序,打开时没有任何问题。
我编写了这段代码
class MainActivity : ComponentActivity() {
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AndroidView(
factory = { context ->
WebView(context).apply {
webViewClient = WebViewClient()
settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.loadWithOverviewMode = true
settings.useWideViewPort = true
settings.allowContentAccess = true
settings.allowFileAccess = true
settings.databaseEnabled = true
settings.loadsImagesAutomatically = true
settings.javaScriptCanOpenWindowsAutomatically = true
settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
isScrollbarFadingEnabled = true
}
},
update = {
it.loadUrl("https://www.odoo.com/")
}
)
}
}
}
什么是正确的设置?
很简单。但很棘手。 我必须加载
WebView
的数据才能加载 Odoo 网站。
update = {
val s = "<!DOCTYPE html><html><head><style>html, body { margin: 0; padding: 0; height: 100%; }</style></head><body><div style='height: 100%; background-color: white;'></div></body></html>"
it.loadData(s, "text/html", "UTF-8")
it.loadUrl("https://www.odoo.com/")
}
这是完整的代码。我可能不需要所有这些,但有人可能会发现这很有用。
class MainActivity : ComponentActivity() {
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AndroidView(
factory = { context ->
WebView(context).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT)
webViewClient = WebViewClient()
settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.loadWithOverviewMode = true
settings.useWideViewPort = true
settings.allowContentAccess = true
settings.allowFileAccess = true
settings.databaseEnabled = true
settings.loadsImagesAutomatically = true
settings.javaScriptCanOpenWindowsAutomatically = true
settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
settings.offscreenPreRaster = true
settings.cacheMode = WebSettings.LOAD_DEFAULT
isScrollbarFadingEnabled = true
}
},
update = {
val s = "<!DOCTYPE html><html><head><style>html, body { margin: 0; padding: 0; height: 100%; }</style></head><body><div style='height: 100%; background-color: white;'></div></body></html>"
it.loadData(s, "text/html", "UTF-8")
it.loadUrl("https://www.odoo.com/")
}
)
}
}
}