前言
Android中WebView打卡前端頁面時受到網路環境,頁面內容大小的影響有時候會讓用戶等待很久。顯示一個加載進度條可以提升很大的體驗。微信內訪問H5頁面加載效果不錯,效仿著寫了一個。
1.實現
1-1.自定義類繼承WebView類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
class ProgressWebView(context: Context, attr: AttributeSet) : WebView(context, attr) { /** *xml布局中使用,所以用兩個構造參數的構造函數 */ private var progressBar: ProgressBar? = null /** *初始化屬性操作 */ init { /** *設置ProgressBar是橫向 */ progressBar = ProgressBar(context, null , android.R.attr.progressBarStyleHorizontal) /** *設置進度條屬性 */ progressBar!!.progressDrawable = context.resources.getDrawable(R.drawable.webview_hori_progress) /** *設置ProgressBar的布局參數 */ val layoutParams = FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 10 , 0 ) /** *綁定參數 */ progressBar!!.layoutParams = layoutParams /** *將ProgressBar添加到WebView上 默認頭部 */ addView(progressBar) /** *設置WebView輔助類WebChromeClient,獲取實時加載進度 */ 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 /** *設置進度參數 */ progressBar!!.progress = progress } } }) } } |
看下設置的加載進度條的屬性,webview_hori_progress.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
< layer-list xmlns:android = "http://schemas.android.com/apk/res/android" > <!--最下層item屬性--> < item > < shape > <!--無圓角--> < corners android:radius = "0dp" /> <!--線條顏色--> < gradient android:endColor = "#FFE4E3E3" android:startColor = "#FFE4E3E3" /> </ shape > </ item > <!--上層item屬性--> < item > < clip > < shape > <!--無圓角--> < corners android:radius = "0dip" /> <!--線條顏色 漸變,由深到淺--> < gradient android:centerColor = "#96EF1627" android:endColor = "#50F53D4B" android:startColor = "#FFEF1627" /> </ shape > </ clip > </ item > </ layer-list > |
1-2.xml 布局中引用
1
2
3
4
|
< com.ypl.csdndemo.ProgressWebView android:id = "@+id/wvProgress" android:layout_width = "match_parent" android:layout_height = "match_parent" /> |
1-3.代碼實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/** *android kotlin 的拓展,導入此包 使用到的組件不用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設置屬性 */ val setting = wvProgress.settings /** *本地緩存無則網絡 */ setting.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK /** *設置識別javascript代碼 */ setting.javaScriptEnabled = true /** *縱向scrollbar去除 */ wvProgress.isVerticalScrollBarEnabled = false /** *加載頁面 */ wvProgress.loadUrl( "https://blog.csdn.net/" ) } } |
2.效果圖
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_17470165/article/details/80636822