Preface
If you do not use the built-in TitleBar (that is, the Activity is set to @ android: style/Theme. noTitleBar), you need to write the progress bar by yourself. Here we encapsulate a custom control and a public Activity that loads the webpage for ease of use.
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com
Farmer's uncle: http://over140.cnblogs.com
Body
I,
Ii. Custom Controls
/**
* WebView with progress bar
* @ Author: farmer's uncle
* @ See http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
*
*/
@ SuppressWarnings ("deprecation ")
Public class ProgressWebView extends WebView {
Private ProgressBar progressbar;
Public ProgressWebView (Context context, AttributeSet attrs ){
Super (context, attrs );
Progressbar = new ProgressBar (context, null, android. R. attr. progressBarStyleHorizontal );
Progressbar. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, 3, 0, 0 ));
AddView (progressbar );
// SetWebViewClient (new WebViewClient (){});
SetWebChromeClient (new WebChromeClient ());
}
Public class WebChromeClient extends android. webkit. WebChromeClient {
@ Override
Public void onProgressChanged (WebView view, int newProgress ){
If (newProgress = 100 ){
Progressbar. setVisibility (GONE );
} Else {
If (progressbar. getVisibility () = GONE)
Progressbar. setVisibility (VISIBLE );
Progressbar. setProgress (newProgress );
}
Super. onProgressChanged (view, newProgress );
}
}
@ Override
Protected void onScrollChanged (int l, int t, int oldl, int oldt ){
LayoutParams lp = (LayoutParams) progressbar. getLayoutParams ();
Lp. x = l;
Lp. y = t;
Progressbar. setLayoutParams (lp );
Super. onScrollChanged (l, t, oldl, oldt );
}
}
3. Loading public Activity of a webpage
/**
* Webpage Activity Loading
*
* @ Author: farmer's uncle
* @ See http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
*
*/
Public class WebActivity extends BaseActivity {
Private ProgressWebView webview;
Private String url;
Private String name;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. commom_web );
//~~~ Get Parameters
Url = getIntent (). getStringExtra ("url ");
Name = getIntent (). getStringExtra ("name ");
//~~~ Bind controls
Webview = (ProgressWebView) findViewById (R. id. webview );
//~~~ Set Data
TitleText. setText (name );
Webview. getSettings (). setJavaScriptEnabled (true );
Webview. setDownloadListener (new DownloadListener (){
@ Override
Public void onDownloadStart (String url, String userAgent, String contentDisposition, String mimetype, long contentLength ){
If (url! = Null & url. startsWith ("http ://"))
StartActivity (new Intent (Intent. ACTION_VIEW, Uri. parse (url )));
}
});
Webview. loadUrl (url );
}
}
Commom_web.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Include layout = "@ layout/include_title"/>
<Com. nmbb. ui. widget. ProgressWebView
Android: id = "@ + id/webview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</LinearLayout>
Iv. Additional instructions
1. You can also optimize it by adding a refresh button in the title bar.
2. If you need to download files from the loaded page, you need to set the setDownloadListener method and customize it according to the actual needs of the project.
3. The custom control is reprinted. If you forget the source, thank you ~~
End
There is no advanced technology, just do it!