Compose中WebView加载HTML内容全屏重叠问题求助
在Jetpack Compose中使用WebView展示HTML内容时出现全屏重叠问题
我在Jetpack Compose界面中使用WebView展示HTML内容,但WebView会重叠整个屏幕。
待展示的HTML内容
"<p><strong></strong></p> <p><span>Test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test <a href=\"https://domopult.ru/\">link</a></span></p> <span><br></span> <p><span><strong>List 1:</strong></span></p> <p></p> <ul> <li>element 1 </li> <li>element 2 </li> <li>element 3 </li> </ul> <p></p> <br> <p><strong>List 2:</strong><br></p> <ol> <li>Element 1 </li> <li>Element 2 </li> <li>Element 3</li> </ol> <p></p>"
我曾尝试使用Html.fromHtml(...)和HtmlCompat.fromHtml(...)结合常规Text(text: AnnotatedString)组件,但这些类无法正确解码<ol>和<p>标签。
当前使用的Compose代码片段
@Composable fun ServiceDescription( service: Service, onImageClick: () -> Unit ) { val serviceDescription = service.description ?: "" Column(modifier = Modifier.fillMaxWidth()) { AttachedImages( images = service.files, onImageClick = { image -> onImageClick() } ) ServiceName( modifier = Modifier.padding(horizontal = 16.dp), serviceName = service.name ?: "" ) if (service.hasPrice()) { ServicePrice( modifier = Modifier.padding(horizontal = 16.dp), price = service.price, productName = service.productName ?: "", priceDescription = service.priceDescription ?: "" ) } Spacer(modifier = Modifier.height(24.dp)) if (service.hasDescription()) { Column( modifier = Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp) ) { Text( modifier = Modifier.padding(horizontal = 16.dp), text = stringResource(id = R.string.request_description), color = ColorDomopult.BasicOnBgPrimary, style = DomopultTypography.body1, fontWeight = FontWeight.W600 ) AndroidView( modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp), factory = { context -> WebView(context).apply { layoutParams = ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT ) settings.javaScriptEnabled = true loadDataWithBaseURL( null, serviceDescription, "html", "utf-8", null, ) } } ) } } } }
现象对比
- 使用
loadDataWithBaseURL加载指定HTML时,WebView会重叠整个屏幕,不符合预期。 - 改用
loadUrl("https://www.google.com")加载网页时,UI显示状态符合预期。
请问如何在WebView中正确展示指定的HTML内容?
内容的提问来源于stack exchange,提问作者Petr
相关产品推荐
相关产品推荐

