android webview增加進度條

主頁面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#1B9A16"
/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="3dip"
android:progressDrawable="@drawable/pg"
android:visibility="gone"
/>
<WebView
android:id="@+id/webview1"
android:layout_below="@id/progressBar1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
view raw main.xml hosted with ❤ by GitHub

progressbar的樣式設定

<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>
view raw pb.xml hosted with ❤ by GitHub


MainActivity的設定

private void init() {
// TODO 自动生成的方法存根
webView=(WebView) findViewById(R.id.webview1);
pg1=(ProgressBar) findViewById(R.id.progressBar1);
webView.setWebViewClient(new WebViewClient(){
//覆写shouldOverrideUrlLoading实现内部显示网页
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO 自动生成的方法存根
view.loadUrl(url);
return true;
}
});
WebSettings seting=webView.getSettings();
seting.setJavaScriptEnabled(true);//设置webview支持javascript脚本
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO 自动生成的方法存根
if(newProgress==100){
pg1.setVisibility(View.GONE);//加载完网页进度条消失
}
else{
pg1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
pg1.setProgress(newProgress);//设置进度值
}
}
});
}
view raw init.java hosted with ❤ by GitHub

留言

這個網誌中的熱門文章

android service作法

html css & bootstrap心得