给WebView添加漂亮的加载进度条

来源:互联网 发布:ubuntu 找不到安装包 编辑:程序博客网 时间:2024/05/22 14:57
<?xml version="1.0" encoding="utf-8"?><RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">        <WebView            android:id="@+id/webView"            android:layout_width="fill_parent"            android:layout_height="fill_parent"/>                <ProgressBar          android:id="@+id/pb"          style="?android:attr/progressBarStyleHorizontal"          android:layout_width="fill_parent"          android:layout_height="3dip"          android:indeterminateOnly="false"          android:max="100"          android:progressDrawable="@drawable/progress_bar_states" >      </ProgressBar></RelativeLayout>

自定义进度条:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:id="@android:id/background">        <shape>            <corners android:radius="2dp" />            <gradient                android:angle="270"                android:centerColor="#E3E3E3"                android:endColor="#E6E6E6"                android:startColor="#C8C8C8" />        </shape>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <corners android:radius="2dp" />                <gradient                    android:centerColor="#4AEA2F"                    android:endColor="#31CE15"                    android:startColor="#5FEC46" />                            </shape>        </clip>    </item></layer-list>

然后就是Activity的主要代码啦:

ProgressBar pb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.xxx);pb = (ProgressBar) findViewById(R.id.pb);pb.setMax(100);WebView webView = (WebView) findViewById(R.id.webview);webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setSupportZoom(true);  webView.getSettings().setBuiltInZoomControls(true);webView.setWebChromeClient(new WebViewClient() );webView.loadUrl("http://www.x.com");}private class WebViewClient extends WebChromeClient {@Overridepublic void onProgressChanged(WebView view, int newProgress) {pb.setProgress(newProgress);if(newProgress==100){pb.setVisibility(View.GONE);}super.onProgressChanged(view, newProgress);}}
0 0