WebView无法正确显示

时间:2013-07-14 18:21:29

标签: android webview android-webview

WebView未正确显示网站。任何帮助都会很棒! 代码已经在所有其他网站上使用了。不确定这个问题是什么。我应该添加什么?适用于Chrome和其他浏览器,因此不知道该怎么做。任何帮助都会很棒!

web视图

enter image description here

enter image description here

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);

        }
    }
}

2 个答案:

答案 0 :(得分:5)

一些建议

配置WebView后移动loadUrl()(我在注释中注意到了,但无论如何都应该在那里)。

看起来您的页面中缺少样式。他们无法加载,或者WebView以某种方式被禁用。尝试添加

myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);myWebView.getSettings().setDomStorageEnabled(true);

看起来WebView也缩小了:
尝试删除
myWebView.getSettings().setUseWideViewPort(true); myWebView.getSettings().setLoadWithOverviewMode(true);

而且仅供参考,您实际上并没有使用MyWebViewClient进行任何操作,而是依赖于默认的WebViewClient

答案 1 :(得分:0)

我不知道这是否解决了这个问题,但是当你在WebView上调用loadUrl时,会在后台线程上调用WebViewClient方法。我不认为你可以直接从后台线程调用UI东西。有关示例,请参阅How to start an activity from a thread class in android?