【安卓笔记】WebView控件使用
wv.loadUrl("file:///android_asset/test1.html");
wv.loadUrl("http://blog.csdn.net");注意,访问远程资源必须加上权限:
<uses-permission android:name="android.permission.INTERNET"/>其实除了loadurl方式,你也可以调用LoadData 或者 loadDataWithBaseURL方法。
WebSettings webSettings = wv.getSettings();得到该对象后便可设置一些属性:
(1)setAllowFileAccess(boolean allow); //设置启用或禁止访问文件数据 (2)setBuiltInZoomControls(boolean enabled); //设置是否支持缩放 (3)setDefaultFontSize(int size); //设置默认的字体大小 (4)setJavaScriptEnabled(boolean flag); //设置是否支持JavaScript (5)setSupportZoom(boolean support); //设置是否支持变焦
(1)doUpdateVisitedHistory(WebView view, String url, boolean isReload); //更新历史记录 (2)onFormResubmission(WebView view, Message dontResend, Message resend);//重新请求网页数据 (3)onLoadResource(WebView view, String url); //加载指定网址提供的资源 (4)onPageFinished(WebView view, String url); //网页加载完毕 (5)onPageStarted(WebView view, String url, Bitmap favicon); //网页开始加载 (6)onReceivedError(WebView view, int errorCode, String description, String failingUrl); //报告错误信息
(1)onProgressChanged(WebView view, int newProgress)//监听网页加载进度 (2)onReceivedTitle(WebView view, String title)//监听网页标题 (3)onReceivedIcon(WebView view, Bitmap icon)//监听网页图标小案例:
package com.example.webviewdemo1; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { protected static final String TAG = null; private WebView wv = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv = (WebView) findViewById(R.id.wv); WebSettings settings = wv.getSettings(); settings.setBuiltInZoomControls(true);//设置是否显示缩放工具 settings.setSupportZoom(true);//设置是否支持缩放 settings.setJavaScriptEnabled(true); settings.setDefaultFontSize(15); //加载本地assets目录下的html // wv.loadUrl("file:///android_asset/test1.html"); //加载远程网页 wv.loadUrl("http://blog.csdn.net"); wv.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //让新打开的网页在当前webview显示 view.loadUrl(url); return true; } }); wv.setWebChromeClient(new WebChromeClient() { //获得网页的加载进度(0-100) @Override public void onProgressChanged(WebView view, int newProgress) { if(newProgress <= 100) { Log.i(TAG,"progress"+newProgress); } } //获得网页的标题 @Override public void onReceivedTitle(WebView view, String title) { Log.i(TAG,"titile="+title); } @Override public void onReceivedIcon(WebView view, Bitmap icon) { // TODO Auto-generated method stub super.onReceivedIcon(view, icon); } }); } }演示效果:
logcat打印的日志:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。