通过使用Compose View的aspectRatio modifier来解决这个问题。
示例代码:
@Composable
fun WebViewContainer(url: String) {
val webView = remember {
WebView()
}
AndroidView(
factory = { context ->
webView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
webView.loadUrl(url)
webView.settings.javaScriptEnabled = true
//设置webview的缩放类型
webView.settings.useWideViewPort = true
webView.settings.loadWithOverviewMode = true
//添加webview的设置
webView.settings.domStorageEnabled = true
webView.settings.databaseEnabled = true
webView.settings.setSupportZoom(true)
webView.settings.builtInZoomControls = true
webView
},
update = { view ->
view.loadUrl(url)
}
)
Box(modifier = Modifier.aspectRatio(1.7f)) {
AndroidView(
factory = { context ->
webView.settings.javaScriptEnabled = true
webView.webViewClient = WebViewClient()
webView.webChromeClient = WebChromeClient()
webView
},
update = { view ->
view.loadUrl(url)
}
)
}
}