This article describes the method of adding progress bars to Android programming in WebView. Share it for your reference, as follows:
Standard XML interface
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android: style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="8dip" android:indeterminateOnly="false" android:max="100" android:progressDrawable="@drawable/progress_bar_states" > </ProgressBar> <WebView android: android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
There are two controls declared above, one is progressBar and the other is webview. Progressbar is used to display the loading progress of the webview control.
It is worth noting that the progressdrawable property we rewritten slightly beautifies the original ugly load bar. The following is the xml code:
<layer-list xmlns:andro> <item android:> <shape> <gradient android:startColor="#ff0000" android:centerColor="#ffa600" android:endColor="#ff5500" /> </shape> </item> <item android:> <clip> <shape> <gradient android:startColor="#234" android:centerColor="#234" android:endColor="#a24" /> </shape> </clip> </item> <item android:> <clip> <shape> <gradient android:startColor="#33000001" android:centerColor="#40000000" android:endColor="#44000000" /> </shape> </clip> </item> </layer-list>
Here is the java code for Activity:
ProgressBar pb; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); pb = (ProgressBar) findViewById(); (100); WebView webView = (WebView) findViewById(); ().setJavaScriptEnabled(true); ().setSupportZoom(true); ().setBuiltInZoomControls(true); (new WebViewClient() ); (""); } private class WebViewClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { (newProgress); if(newProgress==100){ (); } (view, newProgress); } }
The key point is to rewrite the onprogressChange method in webchromeclient, so that we can control the progress of progress. Isn't it very convenient? JD.com does the same, go and try it.
For more information about Android related content, please check out the topic of this site:Android View View Tips Summary》、《A summary of Android development animation skills》、《Android programming activity operation skills summary》、《Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.