Android編程實(shí)現(xiàn)WebView添加進(jìn)度條的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)WebView添加進(jìn)度條的方法。分享給大家供大家參考,具體如下:
標(biāo)準(zhǔn)的XML界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/pb" 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:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
上面聲明了兩個(gè)控件,一個(gè)是progressBar 一個(gè)是 webview,progressbar用來顯示webview控件的加載進(jìn)度的
值得注意的是我們重寫的progressdrawable這個(gè)屬性,把原來難看的加載條,稍稍美化了一些,下面就是xml代碼:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#ff0000"
android:centerColor="#ffa600"
android:endColor="#ff5500"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<gradient
android:startColor="#234"
android:centerColor="#234"
android:endColor="#a24"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#33000001"
android:centerColor="#40000000"
android:endColor="#44000000"
/>
</shape>
</clip>
</item>
</layer-list>
下面是Activity的java代碼:
ProgressBar pb;
@Override
protected 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 {
@Override
public void onProgressChanged(WebView view, int newProgress) {
pb.setProgress(newProgress);
if(newProgress==100){
pb.setVisibility(View.GONE);
}
super.onProgressChanged(view, newProgress);
}
}
關(guān)鍵地方是重寫了一個(gè)webchromeclient中的onprogressChange方法,這樣我們就能控制progress的進(jìn)度啦,是不是很方便的,京東也是這么干的哦,快去試一試吧
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android開發(fā)動(dòng)畫技巧匯總》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
- Android自定義帶進(jìn)度條WebView仿微信加載過程
- Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例
- Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
- android實(shí)現(xiàn)用戶體驗(yàn)超棒的微信WebView進(jìn)度條
- Android 帶進(jìn)度條的WebView 示例代碼
- Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
- Android WebView線性進(jìn)度條實(shí)例詳解
- Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法
- Android WebView實(shí)現(xiàn)頂部進(jìn)度條
相關(guān)文章
Android設(shè)計(jì)模式之代理模式Proxy淺顯易懂的詳細(xì)說明
Android設(shè)計(jì)模式之代理模式也是平時(shí)比較常用的設(shè)計(jì)模式之一,代理模式其實(shí)就是提供了一個(gè)新的對(duì)象,實(shí)現(xiàn)了對(duì)真實(shí)對(duì)象的操作,或成為真實(shí)對(duì)象的替身2018-03-03
Android GridView實(shí)現(xiàn)動(dòng)畫效果實(shí)現(xiàn)代碼
這篇文章主要介紹了 Android GridView實(shí)現(xiàn)動(dòng)畫效果實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android ReboundScrollView仿IOS拖拽回彈效果
這篇文章主要為大家詳細(xì)介紹了Android ReboundScrollView仿IOS拖拽回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)現(xiàn)為GridView添加邊框效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)為GridView添加邊框效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android向node.js編寫的服務(wù)器發(fā)送數(shù)據(jù)并接收請(qǐng)求
這篇文章主要為大家詳細(xì)介紹了Android向node.js編寫的服務(wù)器發(fā)送數(shù)據(jù),并接收請(qǐng)求,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android實(shí)現(xiàn)基于ViewPager的無限循環(huán)自動(dòng)播放帶指示器的輪播圖CarouselFigureView控件
這篇文章主要介紹了Android實(shí)現(xiàn)基于ViewPager的無限循環(huán)自動(dòng)播放帶指示器的輪播圖CarouselFigureView控件,需要的朋友可以參考下2017-02-02
Android手機(jī)聯(lián)系人帶字母索引的快速查找
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)聯(lián)系人帶字母索引的快速查找實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-03-03

