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

Accompanist WebView启动未占满最大尺寸且无法滚动问题求助

Compose中Accompanist WebView加载页面无法占满全屏问题

在Compose中使用Accompanist WebView加载目标URL时,页面启动后仅占不到半个屏幕空间,但该网站在Google Chrome及其他浏览器中显示正常。

完整首页代码

....
@SuppressLint("SetJavaScriptEnabled")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(
    goToSettingsScreen: () -> Unit,
    viewModel: HomeViewModel = hiltViewModel()
) {
    val state = viewModel.state

    val preferences = runBlocking { viewModel.userPreferences.first() }

    val webViewState = rememberWebViewState(url = preferences.url)
    //val scrollState = rememberScrollState()

    val mContext = LocalContext.current
    //Log.i("prefval", preferences.url)

    Scaffold(
        topBar = {
            CustomTopAppBar(
                toggleMenu = { viewModel.toggleDropdownMenu() },
                isMenuOpen = state.value.isHomeDropdownMenuOpen,
                goToSettingsScreen = {
                    goToSettingsScreen()
                    viewModel.toggleDropdownMenu()
                }
            )
        }
    ) { paddingValues ->

        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(top = paddingValues.calculateTopPadding())
                //.verticalScroll(scrollState)
        ) {
            WebView(
                state = webViewState,
                modifier = Modifier
                    .fillMaxSize(),
                onCreated = {
                    it.settings.javaScriptEnabled = true
                    it.settings.allowFileAccess = true
                    it.fitsSystemWindows = true
                    it.setDownloadListener(
                        object : DownloadListener {
                            override fun onDownloadStart(
                                url: String?,
                                userAgent: String?,
                                contentDisposition: String?,
                                mimetype: String?,
                                contentLength: Long
                            ) {
                                val request: DownloadManager.Request =
                                    DownloadManager.Request(Uri.parse(url))
                                request.apply {
                                    setMimeType(mimetype)
                                    setDescription("Downloading file")
                                    addRequestHeader("UserAgent", userAgent)
                                    setTitle(
                                        URLUtil.guessFileName(
                                            url,
                                            contentDisposition,
                                            mimetype
                                        )
                                    )
                                    setNotificationVisibility(
                                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
                                    )
                                }
                                val downloadManager = mContext.getSystemService(
                                    Context.DOWNLOAD_SERVICE
                                ) as DownloadManager?
                                downloadManager?.enqueue(request)
                                Toast.makeText(mContext, "Downloading file", Toast.LENGTH_LONG)
                                    .show()
                            }
                        }
                    )
                }
            )
        }
    }
}
....

截图说明

  • 启动时页面未占满全屏
    启动时页面未占满全屏
  • 触发登录错误(黄色提示框)后,页面尺寸有所调整但仍未占满
    触发登录错误后页面尺寸调整
  • 触发更大的错误提示框后,页面才完全适配尺寸,但尝试滚动查看内容时,滚动条出现却无法滚动
    触发大错误提示框后页面适配但无法滚动

补充说明

在使用Accompanist WebView之前,曾尝试通过AndroidView组合项实现原生WebView,代码逻辑基本一致。最初启动时页面能占满全屏,但添加垂直滚动修饰符解决滚动问题后,出现了和现在相同的启动时未占满屏幕的问题。

内容的提问来源于stack exchange,提问作者Lydian12

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 06:40:34