溫馨提示×

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

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

怎么在Android中實(shí)現(xiàn)微信加載H5頁(yè)面進(jìn)度條

發(fā)布時(shí)間:2021-06-09 17:10:58 來(lái)源:億速云 閱讀:254 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

本篇文章給大家分享的是有關(guān)怎么在Android中實(shí)現(xiàn)微信加載H5頁(yè)面進(jìn)度條,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

1-1.自定義類(lèi)繼承WebView類(lèi)

class ProgressWebView(context: Context, attr: AttributeSet) : WebView(context, attr) {

 /**
 *xml布局中使用,所以用兩個(gè)構(gòu)造參數(shù)的構(gòu)造函數(shù)
 */

 private var progressBar: ProgressBar? = null

 /**
 *初始化屬性操作
 */
 init {

 /**
  *設(shè)置ProgressBar是橫向
  */
 progressBar = ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal)

 /**
  *設(shè)置進(jìn)度條屬性
  */
 progressBar!!.progressDrawable = context.resources.getDrawable(R.drawable.webview_hori_progress)

 /**
  *設(shè)置ProgressBar的布局參數(shù)
  */
 val layoutParams = FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 10, 0)

 /**
  *綁定參數(shù)
  */
 progressBar!!.layoutParams = layoutParams

 /**
  *將ProgressBar添加到WebView上 默認(rèn)頭部
  */
 addView(progressBar)

 /**
  *設(shè)置WebView輔助類(lèi)WebChromeClient,獲取實(shí)時(shí)加載進(jìn)度
  */
 setWebChromeClient(object : WebChromeClient() {
  override fun onProgressChanged(webview: WebView?, progress: Int) {
  super.onProgressChanged(webview, progress)

  Log.d("progressView", progress.toString())
  if (progress == 100)
   progressBar!!.visibility = View.GONE
  else {
   progressBar!!.visibility = View.VISIBLE

   /**
   *設(shè)置進(jìn)度參數(shù)
   */
   progressBar!!.progress = progress

  }
  }

 })

 }
}

看下設(shè)置的加載進(jìn)度條的屬性,webview_hori_progress.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

 <!--最下層item屬性-->
 <item>
 <shape>
  <!--無(wú)圓角-->
  <corners android:radius="0dp" />
  <!--線條顏色-->
  <gradient
  android:endColor="#FFE4E3E3"
  android:startColor="#FFE4E3E3" />
 </shape>
 </item>

 <!--上層item屬性-->
 <item>
 <clip>
  <shape>
  <!--無(wú)圓角-->
  <corners android:radius="0dip" />
  <!--線條顏色 漸變,由深到淺-->
  <gradient
   android:centerColor="#96EF1627"
   android:endColor="#50F53D4B"
   android:startColor="#FFEF1627" />
  </shape>
 </clip>
 </item>
</layer-list>

1-2.xml 布局中引用

 <com.ypl.csdndemo.ProgressWebView
 android:id="@+id/wvProgress"
 android:layout_width="match_parent"
 android:layout_height="match_parent"/>

1-3.代碼實(shí)現(xiàn)

 /**
 *android kotlin 的拓展,導(dǎo)入此包 使用到的組件不用findViewById
 */
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)

 setContentView(R.layout.activity_main)

 /**
  *WebView設(shè)置屬性
  */
 val setting = wvProgress.settings

 /**
  *本地緩存無(wú)則網(wǎng)絡(luò)
  */
 setting.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK

 /**
  *設(shè)置識(shí)別javascript代碼
  */
 setting.javaScriptEnabled = true

 /**
  *縱向scrollbar去除
  */
 wvProgress.isVerticalScrollBarEnabled =false

 /**
  *加載頁(yè)面
  */
 wvProgress.loadUrl("https://blog.csdn.net/")
 }
}

以上就是怎么在Android中實(shí)現(xiàn)微信加載H5頁(yè)面進(jìn)度條,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

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

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

AI