使用webview显示html页面时,
在完成渲染之前会有几秒钟的空白页。
为了获得更好的用户体验,有没有办法让它在几秒钟内显示加载图像?
答案 0 :(得分:21)
试试这段代码。在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);
}
});
答案 1 :(得分:20)
试试这个
......
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);
}
});
...
答案 2 :(得分:11)
尝试使用此代码显示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;
}
}
});
答案 3 :(得分:2)
这对我来说非常有用 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"
}
答案 4 :(得分:1)
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();
});