的WebView显示不正确 [英] WebView not showing correctly

查看:166
本文介绍了的WebView显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的WebView 显示不正确的网站。任何帮助将是巨大的!
在code香港专业教育学院使用的所有其他网站的工作。不知道什么问题。任何事情,我要补充的吗?在Chrome的工作以及和其他浏览器,因此不知道该怎么办。任何帮助将是巨大的!

的WebView

浏览器

 公共类网站延伸活动{
    的WebView myWebView;
    的LinearLayout根;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.website);        myWebView =(的WebView)findViewById(R.id.webView);        根=(的LinearLayout)findViewById(R.id.root);        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(真);       myWebView.addJavascriptInterface(新WebAppInterface(本),机器人);
           myWebView.setWebViewClient(新WebViewClient());
           。myWebView.getSettings()setBuiltInZoomControls(真);
           。myWebView.getSettings()setSupportZoom(真);
           。myWebView.getSettings()setCacheMode(WebSettings.LOAD_DEFAULT);
                   myWebView.getSettings()setDomStorageEnabled(真)。           myWebView.loadUrl(http://dspart.org/);        getActionBar()setDisplayHomeAsUpEnabled(真)。
    }    公共类WebAppInterface {
        上下文mContext;        / **实例化界面和设置上下文* /
        WebAppInterface(上下文C){
            mContext = C;
        }        / **显​​示从网页举杯* /
        @JavascriptInterface
        公共无效showToast(字符串吐司){
            Toast.makeText(mContext,烤面包,Toast.LENGTH_SHORT).show();
        }        私有类MyWebViewClient扩展WebViewClient {
            @覆盖
            公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
                如果(Uri.parse(URL).getHost()。等于(http://dspart.org)){
                    //这是我的网站,所以不要覆盖;让我的的WebView
                    //加载页面
                    返回false;
                }
                //否则,该链接不上我的网站页面,因此推出
                //处理的URL另一个活动
                意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL));
                startActivity(意向);
                返回true;
            }        }
    }    @覆盖
    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        如果(event.getAction()== KeyEvent.ACTION_DOWN){
            开关(键code){
            案例KeyEvent.KEY code_BACK:
                如果(myWebView.canGoBack()){
                    myWebView.goBack();
                }其他{
                    root.removeView(myWebView);
                    myWebView.removeAllViews();
                    myWebView.destroy();
                    this.finish();
                }
                返回true;
            }
        }
        返回super.onKeyDown(键code,事件);
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            NavUtils.navigateUpFromSameTask(本);
            root.removeView(myWebView);
            myWebView.removeAllViews();
            myWebView.destroy();
            this.finish();
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);        }
    }
}


解决方案

一些建议

移动使用loadURL()配置您的WebView后(我注意到,在评论,但它应该是那里不管)。

它看起来像的样式从您的页面中缺少。他们要么加载失败,或在某种程度上正在由的WebView 禁用。尝试添加

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

myWebView.getSettings()setDomStorageEnabled(真);

它也像web视图被缩小:结果
尝试删除结果
myWebView.getSettings()setUseWideViewPort(真)。
        。myWebView.getSettings()setLoadWithOverviewMode(真);

和仅供参考,您没有使用实际 MyWebViewClient 任何东西,而是依赖于默认 WebViewClient

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

        }
    }
}

解决方案

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 removing
myWebView.getSettings().setUseWideViewPort(true); myWebView.getSettings().setLoadWithOverviewMode(true);

And FYI, you're not actually using MyWebViewClient for anything, and instead relying on the default WebViewClient.

这篇关于的WebView显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆