在Android中使用Webview.loadUrl进行渲染时如何显示加载栏?

使用webview显示html页面时,

在完成渲染之前会有几秒钟的空白页面。

为了获得更好的用户体验,有没有办法让它在几秒钟内显示加载图像?

尝试这个

...... webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle(R.string.app_name); } }); ... 

试试这段代码。 在onPageStarted()显示progressDialog或图像或布局,并隐藏在onPageFinished()

  webview.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO show you progress image super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { // TODO hide your progress image super.onPageFinished(view, url); } }); 

尝试使用此代码显示ProgressDialog

  webView.setWebChromeClient(new WebChromeClient() { private ProgressDialog mProgress; @Override public void onProgressChanged(WebView view, int progress) { if (mProgress == null) { mProgress = new ProgressDialog(getActivity()); mProgress.show(); } mProgress.setMessage("Loading " + String.valueOf(progress) + "%"); if (progress == 100) { mProgress.dismiss(); mProgress = null; } } }); 

这对我在KOTLIN有用

 private fun setupWebView() { val webViewClient: WebViewClient = object: WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { view?.loadUrl(request?.url.toString()) return super.shouldOverrideUrlLoading(view, request) } override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { showProgressDialog() super.onPageStarted(view, url, favicon) } override fun onPageFinished(view: WebView?, url: String?) { hideProgressDialog() super.onPageFinished(view, url) } } webView.webViewClient = webViewClient webView.settings.javaScriptEnabled = true webView.settings.defaultTextEncodingName = "utf-8" } 

简单的Java代码 –

  webView.setWebViewClient(new WebViewClient{ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { pdialog.show(); } @Override public void onPageFinished(WebView view, String url) { pdialog.hide(); });