WebView是android的一个组件,它的内核是基于开源WebKit引擎。如果我们对WebView进行一些美化、包装,可以非常轻松的开发出自己的浏览器。
下面是我写的一个例子:
java文件:
package com.dannyAndroid.webviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemo extends Activity {
private WebView webview = null;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.web);
webview.getSettings().setJavaScriptEnabled(true);// 开启Javascript支持
webview.getSettings().setLoadsImagesAutomatically(true);// 设置可以自动加载图片
webview.setHorizontalScrollBarEnabled(true);//设置水平滚动条
webview.setVerticalScrollBarEnabled(false);//设置竖直滚动条
String url = "http://10.*.*.*:8090/webapp/";
webview.loadUrl(url);
//如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接。
//需要给WebView添加一个事件监听对象(WebViewClient),并重写shouldOverrideUrlLoading方法
webview.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dannyAndroid.webviewdemo"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".WebViewDemo"
android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
*禁止超链接的时候调用其他浏览器
如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接。需要给WebView添加一个事件监听对象(WebViewClient),并重写shouldOverrideUrlLoading方法 (详见代码)
*设置滚动条
webview.setHorizontalScrollBarEnabled(true);//设置水平滚动条,true表示允许使用
webview.setVerticalScrollBarEnabled(false);//设置竖直滚动条 ,false表示禁止使用
*禁止横竖屏切换的时候,重新加载网页
需要在AndroidManifest.xml中添加android:configChanges="keyboardHidden|orientation|screenSize" (详见代码)
参考:
http://www.2cto.com/kf/201108/101518.html
http://blog.csdn.net/boyupeng/article/details/6212651