我在Android中使用webView,并尝试在API级别大于或等于19的Android中签入。我的代码在API级别大于19的情况下可以正常工作,但WebView在API级别19中未打开。有人可以检查一下。
这是代码:
public boolean loadingFinished = true;//is loading finished
public boolean redirect = false;// redirect to page
/**
* {@inheritDoc}
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.web_view, container, false);
mWebView = (WebView) rootView.findViewById(R.id.webView);
//initializing views
backButton = (BUtton) rootView.findViewById(R.id.web_view_back_button);
titleTextView = (TextView) rootView.findViewById(R.id.web_view_textView);
progressBar = (ProgressBar) rootView.findViewById(R.id.web_view_progress_bar);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
mWebView.setVisibility(View.GONE);//Visible the view after full loaded
mWebView.setInitialScale(1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setScrollbarFadingEnabled(false);
//check for network connection if not available show error msg!!!
if (!NetworkService.getConnectivityInfo().isConnected(getContext())) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getContext(), getContext().getString(R.string.no_network_msg), Toast.LENGTH_SHORT).show();
} else {
mWebView.loadUrl("https://www.google.co.in");
}
progressBar.setVisibility(View.VISIBLE);
mWebView.setWebViewClient(new WebViewClient() {
/**
* {@inheritDoc}
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(urlNewString);
return true;
}
/**
* {@inheritDoc}
*/
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
loadingFinished = false;
}
/**
* {@inheritDoc}
*/
@Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
progressBar.setVisibility(View.GONE);
//After page is loaded then whole web-view is visible
mWebView.setVisibility(View.VISIBLE);
} else {
redirect = false;
}
}
});
return rootView;
}
最佳答案
在API级别19中,setInitialScale(int scaleInPercent)函数不起作用。这就是为什么代码在大于19的API级别上运行良好的原因。从我的代码中删除mWebView.setInitialScale(1);
后,它在API级别19中也可以正常工作。