我有 ModalBottomSheetLayout 及其加载 web 视图。
但是 ModalBottomSheetLayout 只显示了部分 webview 内容
如何根据 webview 上加载内容的大小更改 ModalBottomSheetLayout?
在 ModalBottomSheetLayout 上将只有一个 webview。
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomSheetScreen(url: String, content: @Composable (ModalBottomSheetState) -> Unit) {
val bottomSheetState =
rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden,
animationSpec = tween(
durationMillis = 500,
delayMillis = 0,
easing = FastOutLinearInEasing
),
confirmValueChange = {
true
})
ModalBottomSheetLayout(
sheetState = bottomSheetState,
sheetShape = RoundedCornerShape(18.dp),
sheetContent = {
Column() {
WebView(url = url)
}
},
) {
content(bottomSheetState)
}
}
我不知道 webview 内容的大小。
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun WebView(url: String) {
AndroidView(factory = {
WebView(it).apply {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
settings.builtInZoomControls = true
settings.javaScriptEnabled = true
settings.setSupportZoom(true)
settings.userAgentString
webChromeClient = WebChromeClient()
//webViewClient = object : WebChromeClient() {}
webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
}
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
return true
}
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
}
}
loadUrl(url)
}
}, update = {
//it.loadUrl(url)
// it.layoutParams.height = it.height
})
}
只找到这样的解决方案
@Composable
fun WebView(url: String, onClose: () -> Unit) {
val state = rememberWebViewState(url)
val context = LocalContext.current
val webViewClient = remember {
object : AccompanistWebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
return true
}
}
}
val chromeViewClient = remember {
object : AccompanistWebChromeClient() {
override fun onCloseWindow(window: WebView?) {
super.onCloseWindow(window)
}
}
}
WebView(
state = state,
onCreated = { webView ->
webView.settings.javaScriptEnabled = true
},
client = remember { webViewClient },
chromeClient = remember { chromeViewClient }
)
}