国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現WebView添加進度條的方法

2019-12-12 03:48:22
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現WebView添加進度條的方法。分享給大家供大家參考,具體如下:

標準的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>

上面聲明了兩個控件,一個是progressBar 一個是 webview,progressbar用來顯示webview控件的加載進度的

值得注意的是我們重寫的progressdrawable這個屬性,把原來難看的加載條,稍稍美化了一些,下面就是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;@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 { @Override public void onProgressChanged(WebView view, int newProgress) {  pb.setProgress(newProgress);  if(newProgress==100){   pb.setVisibility(View.GONE);  }  super.onProgressChanged(view, newProgress); }}

關鍵地方是重寫了一個webchromeclient中的onprogressChange方法,這樣我們就能控制progress的進度啦,是不是很方便的,京東也是這么干的哦,快去試一試吧

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android開發動畫技巧匯總》、《Android編程之activity操作技巧總結》、《Android布局layout技巧總結》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特中旗| 航空| 桃园市| 分宜县| 开阳县| 交口县| 分宜县| 南华县| 祁东县| 江华| 江达县| 长汀县| 右玉县| 绥滨县| 平阴县| 汶川县| 鄯善县| 会宁县| 湘乡市| 安多县| 南陵县| 兖州市| 扶绥县| 新和县| 公主岭市| 水城县| 盐亭县| 襄城县| 上饶市| 巴南区| 南乐县| 陈巴尔虎旗| 普兰县| 广州市| 牙克石市| 平南县| 汝州市| 临猗县| 黄平县| 赣州市| 东安县|