You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 01:28:18