android - 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 显示空白页面。
有人对这个问题有任何想法吗?
解决方案
推荐阅读
- python - 如何从文件中读取内容,然后将它们保存在文件中?
- android - 使用改造从 API 获取数据
- python - MARK Unpickling 错误:如何解决?
- sql - 使用解码更新不起作用,为什么输入空数据?
- css - 从 translateX 开始的 Div 动画 100% 在移动设备上从 0% 开始
- c# - 如何在 C# Windows 窗体的 MS 图表中添加次要刻度线?
- php - php广告中的反序列化函数结果字符串的额外斜杠
- sql-server-2008 - 当表 1 中出现新值时,如何自动更新表 2 中的值
- angular - 我已经为不同的组件创建了模块,但是 loadChildren 不适用于模块
- jquery - JQuery 数据表按钮不工作/不显示