要读取assets下的目录,只需要修改三个地方即可
1.res/layout/activity_main.xml
2.AndroidManifest.xml
3.srccomexamplelcyMainActivity.java
第一步.res/layout/activity_main.xml加入代码,需要在xml布局文件中声明WebView组件
<WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
全部代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
第二部,AndroidManifest.xml 代码
加入代码:由于不是用Intent去调用系统组件,所以我们需要对程序进行权限的允许,在AndroidManifest.xml里对程序开放Inter权限
<uses-permission android:name="android.permission.INTERNET"/>
如果出现 webpage not available 在AndroidManifest.xml可以加入以上代码
全部代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lcy" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.lcy.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
3.srccomexamplelcyMainActivity.java 在Activity中实例化WebView,并且可通过loadUrl(url)方法打开指定url资源,加入代码
public class MainActivity extends Activity { private WebView webView; //private String url = "http://www.baidu.com"; private String url = "file:///android_asset/www/index.html"; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWebView(); } private void initWebView() { webView = (WebView) findViewById(R.id.webview);// 获取控件 webView.loadUrl(url);// 载入指定url(系统自带浏览器,若想用自身webview需要重写方法,提供client) WebSettings webSettings = webView.getSettings();// 获取配置信息 webSettings.setJavaScriptEnabled(true);// 是否允许加载js文件 webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 打开缓存 webView.setWebViewClient(new WebViewClient() { /** * 重写shouldOverrideUrlLoading,返回值若为true则用webview,false则是系统自身浏览器 */ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) {// newProgress当前进度 if (newProgress == 100) { // 加载完毕,关闭进度条 closeProgressDialog(); } else { //加载未完成,显示进度 showProgressDialog(newProgress); } super.onProgressChanged(view, newProgress); } private void closeProgressDialog() { progressDialog.dismiss(); progressDialog=null; } private void showProgressDialog(int newProgress) { if (progressDialog == null) { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("页面正在加载中..请稍后"); progressDialog .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条样式 progressDialog.setProgress(newProgress);//设置进度 progressDialog.show(); } else { progressDialog.setProgress(newProgress); progressDialog.show(); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {// keyCode代表按键的数字标示符 if (keyCode == KeyEvent.KEYCODE_BACK) { if (webView.canGoBack()) { webView.goBack(); return true; } else { System.exit(0); } } return super.onKeyDown(keyCode, event); } }
全部代码(有部分代码不用用管它,增加了public 、private 会自动增加,保证把上面的代码加进去就可以了)
package com.example.lcy; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private WebView webView; //private String url = "http://www.baidu.com"; private String url = "file:///android_asset/www/index.html"; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWebView(); } private void initWebView() { webView = (WebView) findViewById(R.id.webview);// 获取控件 webView.loadUrl(url);// 载入指定url(系统自带浏览器,若想用自身webview需要重写方法,提供client) WebSettings webSettings = webView.getSettings();// 获取配置信息 webSettings.setJavaScriptEnabled(true);// 是否允许加载js文件 webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 打开缓存 webView.setWebViewClient(new WebViewClient() { /** * 重写shouldOverrideUrlLoading,返回值若为true则用webview,false则是系统自身浏览器 */ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) {// newProgress当前进度 if (newProgress == 100) { // 加载完毕,关闭进度条 closeProgressDialog(); } else { //加载未完成,显示进度 showProgressDialog(newProgress); } super.onProgressChanged(view, newProgress); } private void closeProgressDialog() { progressDialog.dismiss(); progressDialog=null; } private void showProgressDialog(int newProgress) { if (progressDialog == null) { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("页面正在加载中..请稍后"); progressDialog .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条样式 progressDialog.setProgress(newProgress);//设置进度 progressDialog.show(); } else { progressDialog.setProgress(newProgress); progressDialog.show(); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {// keyCode代表按键的数字标示符 if (keyCode == KeyEvent.KEYCODE_BACK) { if (webView.canGoBack()) { webView.goBack(); return true; } else { System.exit(0); } } return super.onKeyDown(keyCode, event); } }
详情查看 http://www.cnblogs.com/lichenwei/p/3959345.html