首页 > 解决方案 > Webview android显示空白页

问题描述

我需要在 android webview 中显示和打开 pdf 文件。我在这里尝试了很多代码,但每次打开片段 webview 时都会显示空白屏幕。

下面是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.ViewerFragment">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

</RelativeLayout>

下面是我的科特林代码:

webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                progressBar.visibility = View.GONE
                super.onPageFinished(view, url)
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                progressBar.visibility = View.VISIBLE
                super.onPageStarted(view, url, favicon)
            }
        }


        webView.settings?.javaScriptEnabled = true
        webView.settings?.javaScriptCanOpenWindowsAutomatically = true
        webView.settings?.builtInZoomControls = true
        webView.settings?.domStorageEnabled = true
        webView.settings?.loadWithOverviewMode = true
        webView.settings?.useWideViewPort = true
        webView.settings?.displayZoomControls = false
        webView.webChromeClient = WebChromeClient()
        webView.loadUrl("http://docs.google.com/gview?embedded=true&url=${url}")

很多时候 pdf 会在几秒钟内显示出来。但是很多时候 onPageFinished 被调用并且 webview 显示空白页面。

有人对这个问题有任何想法吗?

标签: androidkotlinandroid-webviewpdf-viewer

解决方案


推荐阅读