用WebView显示网页非常方便,缺点是速度比较慢,对webView属性也要设置一下,否则可能会出现宽度不匹配等问题。代码并不复杂,如下:
private WebView wv_statistics_html; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected void initViews(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_statistics_html); wv_statistics_html = (WebView) findViewById(R.id.wv_statistics_html); WebSettings webSettings = wv_statistics_html.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setUseWideViewPort(true); webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); webSettings.setDisplayZoomControls(false); webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本 webSettings.setAllowFileAccess(true); // 允许访问文件 webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮 webSettings.setSupportZoom(true); // 支持缩放 webSettings.setLoadWithOverviewMode(true); wv_statistics_html.loadUrl("http://www.pipaw.com/ttxqtxb/168418.html"); wv_statistics_html.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return true; } }); }
本文出自 “移动平台开发” 博客,请务必保留此出处http://liuxudong1001.blog.51cto.com/10877072/1794844
原文地址:http://liuxudong1001.blog.51cto.com/10877072/1794844