webview是常用的组件,特别是当前h5这么流行的情况下,本文介绍webview在项目中的常规用法。一般是webview加载的过程前会对当前网络做判断,没有网络的情况下给个提示。有网络的情况下配一个progressbar显示当前进度,好的,上代码。
第一步:设计页面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include layout="@layout/layout_head" /> <ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="2dip" android:indeterminateOnly="false" android:max="100" android:progressDrawable="@drawable/progress_bar_states_new" > </ProgressBar> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
第二步:编写activity
/** * */ package com.android.inputmethod.latin.activity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import com.android.inputmethod.latin.R; import com.dotc.util.NetworkUtil; /** * @author figo * */ public class CommonProblemActivity extends Activity { private WebView wb; ProgressBar pb; private LinearLayout mLayoutBack; private TextView mTvTitle; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_common_problem); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); initView(); } private void initView() { try { pb = (ProgressBar) findViewById(R.id.pb); pb.setMax(100); mLayoutBack = (LinearLayout) findViewById(R.id.layout_back); mLayoutBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); mTvTitle = (TextView) findViewById(R.id.txt_title); mTvTitle.setText(getString(R.string.lbl_common_problem)); //这样写将直接跳转到浏览器 // wb.loadUrl("http://www.baidu.com"); //使用WebViewClient web页面在当前activity中显示 wb = (WebView) findViewById(R.id.webView); wb.getSettings().setJavaScriptEnabled(true); wb.getSettings().setSupportZoom(true); wb.getSettings().setBuiltInZoomControls(true); wb.setWebChromeClient(new WebViewClient() ); if(!NetworkUtil.isNetworkUseful(CommonProblemActivity.this)) { String customHtml = "<html><body><font color='red'>current network useless!</font></body></html>"; wb.loadData(customHtml, "text/html", "UTF-8"); return; } wb.loadUrl("http://www.baidu.com"); } catch (Exception e) { if (e != null) { e.printStackTrace(); } } } private class WebViewClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { pb.setProgress(newProgress); if(newProgress==100){ pb.setVisibility(View.GONE); } super.onProgressChanged(view, newProgress); } } }
原文地址:http://blog.csdn.net/figo0423/article/details/46522867