在调用的时候很简单,就只需要传递一个url(加载网页的url)和title(显示标题)就可以了,如下所示:
Intent intent = new Intent(MainActivity.this, MainWebViewActivity.class);intent.putExtra("url", "http://blog.csdn.net/qq_20785431");intent.putExtra("title", "我的博客");startActivity(intent);1.接下来主要还是看看重写的带加载条的webview
package com.per.loadingwebviewdome;import android.content.Context;import android.os.Environment;import android.util.AttributeSet;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.ProgressBar;/** * @author: xiaolijuan * @description: 带加载条的webview * @date: 2016-06-03 * @time: 23:34 */public class LoadingWebView extends WebView { private ProgressBar mProgressBar; /*** 网页缓存目录*/ private static final String cacheDirPath = Environment .getExternalStorageDirectory() + "/LoadingWebViewDome/webCache/"; public LoadingWebView(Context context) {super(context, null); } public LoadingWebView(Context context, AttributeSet attrs) {super(context, attrs, 0); } public LoadingWebView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initContext(context); } private void initContext(Context context) {requestFocus();setInitialScale(39);getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//支持通过Javascript打开新窗口getSettings().setJavaScriptEnabled(true);//设置WebView属性,能够执行Javascript脚本getSettings().setUseWideViewPort(true);//将图片调整到适合webview的大小getSettings().setLoadWithOverviewMode(true);// 缩放至屏幕的大小getSettings().setDomStorageEnabled(true);//设置是否启用了DOM Storage APIgetSettings().setDatabaseEnabled(true);//开启database storage API功能getSettings().setDatabasePath(cacheDirPath); //设置数据库缓存路径getSettings().setAppCachePath(cacheDirPath);//设置Application Caches缓存目录getSettings().setAppCacheEnabled(true);//开启Application Caches功能 } /*** 加载网页url** @param url*/ public void loadMessageUrl(String url) {super.loadUrl(url);setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { // 重写此方法表明点击网页里面的链接不调用系统浏览器,而是在本WebView中显示loadUrl(url);//加载需要显示的网页return true; }}); } /*** 添加进度条*/ public void addProgressBar() {mProgressBar = new ProgressBar(getContext(), null,android.R.attr.progressBarStyleHorizontal);mProgressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5, 0, 0));mProgressBar.setProgressDrawable(getContext().getResources().getDrawable(R.drawable.bg_pb_web_loading));addView(mProgressBar);//添加进度条至LoadingWebView中setWebChromeClient(new WebChromeClient());//设置setWebChromeClient对象 } public class WebChromeClient extends android.webkit.WebChromeClient {@Overridepublic void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) {mProgressBar.setVisibility(GONE); } else {if (mProgressBar.getVisibility() == GONE) mProgressBar.setVisibility(VISIBLE);mProgressBar.setProgress(newProgress); } super.onProgressChanged(view, newProgress);} } /*** 回收webview*/ public void destroyWebView() {clearCache(true);clearHistory(); }}我们重写了3个构造方法,默认的布局文件调用的是两个参数的构造方法,所以记得让所有的构造调用我们的三个参数的构造,我们在三个参数的构造中获得自定义View的属性。
<?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/common_top_banner" /> <com.per.loadingwebviewdome.LoadingWebViewandroid:id="@+id/wv_loading"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>2.下面就是通用的带进度条的WebView啦
package com.per.loadingwebviewdome;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.TextView;/** * @author: xiaolijuan * @description: 通用的带进度条的WebView * @date: 2016-06-03 * @time: 23:32 */public class MainWebViewActivity extends Activity implements View.OnClickListener { private ImageView mIvBack; private TextView mTvTitle; private LoadingWebView mLoadingWebView; private String mTitle = ""; private String mUrl = ""; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_webview);initView();initData(); } private void initView() {mIvBack = (ImageView) findViewById(R.id.iv_back);mLoadingWebView = (LoadingWebView) findViewById(R.id.wv_loading);mTvTitle = (TextView) findViewById(R.id.tv_title);mLoadingWebView.addProgressBar();mIvBack.setOnClickListener(this); } private void initData() {mTitle = getIntent().getStringExtra("title");mUrl = getIntent().getStringExtra("url");mLoadingWebView.loadMessageUrl(mUrl);mTvTitle.setText(mTitle); } @Override public void onDestroy() {super.onDestroy();mLoadingWebView.destroyWebView(); } @Override public void onClick(View v) {switch (v.getId()) { case R.id.iv_back:if (mLoadingWebView.canGoBack()) mLoadingWebView.goBack();else { finish();}break;} } /*** 按返回键时, 不退出程序而是返回WebView的上一页面*/ @Override public void onBackPressed() {if (mLoadingWebView.canGoBack()) mLoadingWebView.goBack();else { super.onBackPressed();} }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。