android webview增加進度條
主頁面
progressbar的樣式設定
MainActivity的設定
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
progressbar的樣式設定
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
MainActivity的設定
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);//设置进度值 | |
} | |
} | |
}); | |
} |
留言
張貼留言