标签:
在android应用中,有时要加载一个网页,如果能配上一个进度条就更好了,而android
中提供了其很好的支持,下面是一个例子程序,先帖:
1
2
3
4
|
<?xml version= "1.0" encoding= "utf-8" ?> <WebView xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/webView" android:layout_width= "fill_parent" android:layout_height= "fill_parent" /> |
主程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class WebPageLoader extends Activity { final Activity activity = this ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); WebView webView = (WebView) findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled( true ); webView.getSettings().setSupportZoom( true ); webView.setWebChromeClient( new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setTitle( "Loading..." ); activity.setProgress(progress * 100 ); if (progress == 100 ) activity.setTitle(R.string.app_name); } } ); webView.setWebViewClient( new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true ; } }); } } |
要注意的是,其中的webView的一系列用法,比如 webView.getSettings().setJavaScriptEnabled(true);设置可以使用javscript;
webView.getSettings().setJavaScriptEnabled( true ); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setHorizontalScrollBarEnabled( false ); webView.getSettings().setSupportZoom( true ); webView.getSettings().setBuiltInZoomControls( true ); webView.setInitialScale( 70 ); webView.setHorizontalScrollbarOverlay( true ); |
Android中使用WebView, WebChromeClient和WebViewClient加载网页 (能够执行js)
标签:
原文地址:http://www.cnblogs.com/cmblogs/p/4531417.html