有时需要app里面显示网页,而不调用其他浏览器浏览网页,那么这时就需要WebView控件。这个控件也是很强大的,放大,缩小,前进,后退网页都可以。
//支持javascript
web.getSettings().setJavaScriptEnabled(true);
// 设置可以支持缩放
web.getSettings().setSupportZoom(true);
// 设置出现缩放工具
web.getSettings().setBuiltInZoomControls(true);
//扩大比例的缩放
web.getSettings().setUseWideViewPort(true);
//自适应屏幕
web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
web.getSettings().setLoadWithOverviewMode(true);
这里要说一下,有一些网页需要自身支持缩放,才能缩放网页的。如果不支持,是无法缩放的。
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ProgressBar 8 android:id="@+id/progressBarLoading" 9 style="?android:attr/progressBarStyleHorizontal" 10 android:layout_width="match_parent" 11 android:layout_height="3dp" /> 12 13 <WebView 14 android:id="@+id/webView1" 15 android:layout_width="match_parent" 16 android:layout_height="match_parent" /> 17 18 </LinearLayout>
package com.example.asynchttpclient; import android.os.Bundle; import android.app.Activity; import android.view.KeyEvent; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings.ZoomDensity; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; public class WebActivity extends Activity { private ProgressBar mLoadingProgress; private WebView webView; private String mstrLoginUrl = "http://baidu.com"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); webView = (WebView)findViewById(R.id.webView1); mLoadingProgress = (ProgressBar)findViewById(R.id.progressBarLoading); mLoadingProgress.setMax(100); webView.getSettings().setUseWideViewPort(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); //设置可以支持缩放 webView.getSettings().setDefaultZoom(ZoomDensity.FAR); webView.loadUrl(mstrLoginUrl); // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); //设置加载进度条 view.setWebChromeClient(new WebChromeClientProgress()); return true; } }); } private class WebChromeClientProgress extends WebChromeClient{ @Override public void onProgressChanged(WebView view, int progress) { if (mLoadingProgress != null) { mLoadingProgress.setProgress(progress); if (progress == 100) mLoadingProgress.setVisibility(View.GONE); } super.onProgressChanged(view, progress); } } /** * 按键响应,在WebView中查看网页时,检查是否有可以前进的历史记录。 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Check if the key event was the Back button and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // 返回键退回 webView.goBack(); return true; } // If it wasn't the Back key or there's no web page history, bubble up // to the default // system behavior (probably exit the activity) return super.onKeyDown(keyCode, event); } }
<uses-permission android:name="android.permission.INTERNET"/>