问题
WebView is not showing website correctly. Any help would be great!
The code ive used work on all another site. Not sure whats the issue. Any thing I should add? Works well in chrome and other browsers so don't know what to do. Any help would be great!
WebView
Chrome
public class Website extends Activity {
WebView myWebView;
LinearLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.website);
myWebView = (WebView) findViewById(R.id.webView);
root = (LinearLayout) findViewById(R.id.root);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.loadUrl("http://dspart.org/");
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dspart.org")) {
// This is my web site, so do not override; let my WebView
// load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
回答1:
A few suggestions:
Move loadUrl() after configuring your WebView (I noticed that in the comments, but it should be down there regardless).
It looks like the styles are missing from your page. Either they failed to load, or are somehow being disabled by the WebView. Try adding
myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
and
myWebView.getSettings().setDomStorageEnabled(true);
It also looks like the WebView is zoomed out:
Try removingmyWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
And FYI, you're not actually using MyWebViewClient for anything, and instead relying on the default WebViewClient.
回答2:
I don't know if this fixes the problem, but when you call loadUrl on a WebView the WebViewClient methods are invoked on a background thread. I don't think you can call UI stuff directly from a background thread. See How to start an activity from a thread class in android? for an example.
来源:https://stackoverflow.com/questions/17642285/webview-not-showing-correctly