自定义WebView显示网页标题和加载进度
还是做一个应用用到的技术。很简单的一个就是将网页的标题获取到然后显示在Activity中的title上,并且在加载网页时显示进度条。很简单,详见以下代码:
package demo.androidyue.webview;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class WebViewDemoActivity extends Activity {
private WebView webView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化WebView
this.initWebView();
}
/*
* 初始化WebView
*/
private void initWebView(){
//从布局文件中扩展webView
this.webView=(WebView)this.findViewById(R.id.webview);
this.webView.setWebChromeClient(new chromeClient());
//加载地址 www.zzzyk.com
this.webView.loadUrl("http://www.openapis.net/(S(a0xds4mbqaatqg45vy32parf))/APIs.MainPage.ashx");
}
class chromeClient extends WebChromeClient{
@Override
public void onProgressChanged(WebView view, int newProgress) {
//动态在标题栏显示进度条
WebViewDemoActivity.this.setProgress(newProgress*100);
super.onProgressChanged(view, newProgress);
}
@Override
public void onReceivedTitle(WebView view, String title) {
//设置当前activity的标题栏
WebViewDemoActivity.this.setTitle(title);
super.onReceivedTitle(view, title);
}
}
}
重要的就是设置WebChromeClient,WebChromeClient是WebView的辅助类,用来处理js,favicon和标题等一些操作。
Last Modified:2011-12-06 更多技术文章请浏览 http://thinkblog.sinaapp.com
摘自 BossDarcy的专栏
补充:移动开发 , Android ,