有时,当我使用loadUrl加载我的webview时,在我触摸屏幕或滚动之前,网站才会显示。
就像我有一个webview绘图问题。
Context context = ctx;
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.website);
WebView webView = (WebView) dialog.findViewById(R.id.weburl);
webView.setScrollbarFadingEnabled(false);
//Disable the horizontal scroll bar
webView.setHorizontalScrollBarEnabled(false);
//Enable JavaScript
webView.getSettings().setJavaScriptEnabled(true);
//Set the user agent
webView.getSettings().setUserAgentString("AndroidWebView");
//Clear the cache
webView.clearCache(true);
webView.loadUrl("http://" + WebUrl);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
@Override
public void onPageFinished(WebView view, String url) {
view.refreshDrawableState();
Log.v("FINISH","FINISH");
}
});
有人知道我为什么会遇到这种问题。
答案 0 :(得分:2)
您正在测试哪个版本的Android? 4.1之前版本的Android有时会出现WebViews的这种问题。
将此添加到该Activity的清单中以解决问题:
android:hardwareAccelerated="false"
答案 1 :(得分:1)
在布局中隐藏WebView
:
<WebView
...
android:visibility="invisible"
.../>
现在,在onPageFinished
第一次出现时显示回来:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (webView.getVisibility() != View.VISIBLE) {
webView.setVisibility(View.VISIBLE);
}
}
});
答案 2 :(得分:0)
我不确定,我没有完整的代码,但我认为与此函数中实现的webViewClient
有关:
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
这是官方的定义:
public boolean shouldOverrideUrlLoading (WebView view, String url)
尝试使用您的代码进行测试而不实施shouldOverrideUrlLoading
,或将其设为return true
。