WebView的使用

来源:互联网 发布:算法复杂度分析 例题 编辑:程序博客网 时间:2024/06/07 18:42

Webview是Android里面比较常用的可以将网页放假APP的控件。webview还可以和HTML以及JavaScript结合使用,功能强大。

1、下面贴出最基本的用法。

 @ViewInject(R.id.webView)

private WebView webView;

private String url;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        Log.i("liwei","bundle:"+bundle);
        if (bundle == null) {
            showToast("参数错误");
            return;
        }
            setTitle("");
        url = bundle.getString("url");
        Log.i("liwei", "=== url: " + url);
        initWebView();
    }

    private void initWebView() {
        if (StringUtils.isEmpty(url)) {
            showToast("不存在此信息");
            finish();
            return;
        }
        showDialog();

        // 设置可以支持缩放
       webView.getSettings().setSupportZoom(true);
        //设置默认加载的可视范围是大视野范围
        webView.getSettings().setLoadWithOverviewMode(true);
        //设置可在大视野范围内上下左右拖动,并且可以任意比例缩放
        webView.getSettings().setUseWideViewPort(true);
        webView.loadUrl(url);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    DialogUtils.dismiss(progressDialog);
                }
            }
        });
    }


2、webview自适应屏幕(以下代码是从别人博客直接搬过来的)

webview中右下角的缩放按钮能不能去掉
settings.setDisplayZoomControls(false); //隐藏webview缩放按钮

让Webview加载的页面居中显示有我知道的几种方法

第一种方法:
WebSettings settings = webView.getSettings();
settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
LayoutAlgorithm是一个枚举用来控制页面的布局,有三个类型:
1.NARROW_COLUMNS:可能的话使所有列的宽度不超过屏幕宽度
2.NORMAL:正常显示不做任何渲染
3.SINGLE_COLUMN:把所有内容放大webview等宽的一列中
用SINGLE_COLUMN类型可以设置页面居中显示,页面可以放大缩小,但这种方法不怎么好,有时候会让你的页面布局走样而且我测了一下,只能显示中间那一块,超出屏幕的部分都不能显示。

第二种方法:
//设置加载进来的页面自适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
第一个方法设置webview推荐使用的窗口,设置为true。第二个方法是设置webview加载的页面的模式,也设置为true。
这方法可以让你的页面适应手机屏幕的分辨率,完整的显示在屏幕上,可以放大缩小。
两种方法都试过,推荐使用第二种方法

第三种方法:(主要用于平板,针对特定屏幕代码调整分辨率)
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int mDensity = metrics.densityDpi;
    if (mDensity == 120) {
              settings.setDefaultZoom(ZoomDensity.CLOSE);
          }else if (mDensity == 160) {
              settings.setDefaultZoom(ZoomDensity.MEDIUM);
          }else if (mDensity == 240) {
              settings.setDefaultZoom(ZoomDensity.FAR);
          }

原创粉丝点击