溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中怎么實現(xiàn)一個帶進(jìn)度條的WebView

發(fā)布時間:2021-08-07 13:52:43 來源:億速云 閱讀:131 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么實現(xiàn)一個帶進(jìn)度條的WebView,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

一、自定義ProgressWebView類

public class ProgressWebView extends WebView {  private ProgressBar progressbar;  public ProgressWebView(Context context, AttributeSet attrs) {    super(context, attrs);    progressbar = new ProgressBar(context, null,        android.R.attr.progressBarStyleHorizontal);    progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,        5, 0, 0));    Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states);    progressbar.setProgressDrawable(drawable);    addView(progressbar);    // setWebViewClient(new WebViewClient(){});    setWebChromeClient(new WebChromeClient());    //是否可以縮放    getSettings().setSupportZoom(true);    getSettings().setBuiltInZoomControls(true);  }  public class WebChromeClient extends android.webkit.WebChromeClient {    @Override    public void onProgressChanged(WebView view, int newProgress) {      if (newProgress == 100) {        progressbar.setVisibility(GONE);      } else {        if (progressbar.getVisibility() == GONE)          progressbar.setVisibility(VISIBLE);        progressbar.setProgress(newProgress);      }      super.onProgressChanged(view, newProgress);    }  }  @Override  protected void onScrollChanged(int l, int t, int oldl, int oldt) {    LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();    lp.x = l;    lp.y = t;    progressbar.setLayoutParams(lp);    super.onScrollChanged(l, t, oldl, oldt);  }}

二、布局文件標(biāo)簽寫成自定義的類,使用和一般WebView一致

最后貼一下drawable下的progress_bar_states

<?xml version="1.0" encoding="utf-8"?><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>

關(guān)于Android中怎么實現(xiàn)一個帶進(jìn)度條的WebView就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI