Android Webview與ScrollView的滾動兼容及留白處理的方法
本文介紹了Webview與ScrollView的滾動兼容及留白處理,分享給大家,具體如下:
背景
開發(fā)中我們經(jīng)常會遇到使用網(wǎng)頁來顯示圖文內(nèi)容,而且往往我們會遇到webview嵌套在scrollview的這種情況,這就開始讓人蛋疼了!“為嘛,我的webview加載出來的網(wǎng)頁只顯示很小一點,其他都不顯示了?” ”當(dāng)我重新刷新頁面后,為什么webview會出現(xiàn)留白的情況?“ ----------------- 天啊,難道就不能好好的嗎?!
為了解決項目中這些蛋疼的問題,試過不少方法,網(wǎng)上有說是網(wǎng)頁中使用了不合理的overflow,的確,經(jīng)證實使用不合理的overflow的確會造成網(wǎng)頁加載后在嵌套在scrollview的webview只會顯示很小的高度,那體驗是相當(dāng)?shù)膶擂?。合理使用overflow即可處理這個問題,但是webview留白又如何處理呢?問題先放這兒,我們先說說如何在xml布局中放置webview并設(shè)置他的屬性。
層層遞進,先練基本功
xml中webview嵌套在scrollview中:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:descendantFocusability="blocksDescendants" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.xxxx.NoWrapListView android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"/> <WebView android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>
其中webview要的高度要設(shè)置為:wrap_content, 如有必要可設(shè)置scrollview第一個子容器的這個屬性:
android:descendantFocusability="blocksDescendants"
發(fā)現(xiàn)問題,問題是如何造成的
我們使用webview加載網(wǎng)頁,網(wǎng)頁可能在我們需要的時候會要求我們刷新網(wǎng)頁或者加載新的鏈接,這時候問題就顯現(xiàn)了。由于網(wǎng)頁頁面加載內(nèi)容的長度,或者ajax請求延遲,造成webview只能不斷的增加高度,而當(dāng)網(wǎng)頁高度變小時,webview高度卻不能自適應(yīng)了,那么只能由我們手動的搞些事情了!
解決問題,解決留白,刻不容緩
1、重載WebViewClient,重寫onPageFinished方法。
inner class XWalkWebClient : WebViewClient() { override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { super.onPageStarted(view, url, favicon) isPageLoadSuccess = true } override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) view?.loadUrl("javascript:window.myapp.resize(document.body.getBoundingClientRect().bottom);") //此處調(diào)用了一個注入的js方法用來重載webview高度,可解決初始加載網(wǎng)頁的問題,① } }
2、js注入,初始化注入方法
webBrowser?.addJavascriptInterface(MyAppJavascriptHandler(), "myapp")
inner class MyAppJavascriptHandler { @JavascriptInterface fun resize(documentBodyHeight: Int) { if (isAllowReLayoutBrowser) { (context as? Activity?)?.runOnUiThread { ViewUtil.setViewLayoutParams<FrameLayout.LayoutParams>(webBrowser!!, { it.width = context.resources.displayMetrics.widthPixels it.height = (documentBodyHeight * context.resources.displayMetrics.density).toInt() }) //重寫webview的高度, ② } } } }
網(wǎng)頁端也需要在數(shù)據(jù)加載完成后調(diào)用這個js注入方法
if(window.myapp.resize){ window.myapp.resize(document.body.getBoundingClientRect().bottom); }
備注、解釋:
①. document.body.getBoundingClientRect().bottom: 網(wǎng)頁下邊距離頁面上邊的距離
②. ViewUtil.setViewLayoutParams....方法的實現(xiàn)
/** * 配置控件的布局屬性 * @param view * @param func 處理布局屬性的回調(diào)方法 */ @Suppress("unchecked_cast") @JvmStatic fun <T : ViewGroup.LayoutParams> setViewLayoutParams(view: View, func: (T) -> Unit) = with(this) { val lp: T = view.layoutParams as T func.invoke(lp) view.layoutParams = lp }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android如何讓W(xué)ebView中的HTML5頁面實現(xiàn)視頻全屏播放
- Android編程實現(xiàn)WebView全屏播放的方法(附源碼)
- Android編程使WebView支持HTML5 Video全屏播放的解決方法
- Android使用WebView播放flash的方法
- Android WebView與JS交互全面詳解(小結(jié))
- 詳解android 用webview加載網(wǎng)頁(https和http)
- Android webview如何加載HTML,CSS等語言的示例
- Android關(guān)于WebView中無法定位的問題解決
- Android開發(fā)實現(xiàn)webview中img標(biāo)簽加載本地圖片的方法
- Android studio點擊跳轉(zhuǎn)WebView詳解
- 詳解android webView獨立進程通訊方式
- Android使用WebView實現(xiàn)全屏切換播放網(wǎng)頁視頻功能
相關(guān)文章
Android 對手機網(wǎng)絡(luò)的檢測和監(jiān)聽的方法示例
本篇文章主要介紹了Android 對手機網(wǎng)絡(luò)的檢測和監(jiān)聽的方法示例,主要使用BroadcastReceiver廣播接收器來接收網(wǎng)絡(luò)狀態(tài),現(xiàn)在分享給大家,也給大家做個參考,有興趣的一起來了解一下2018-03-03Android自定義HorizontalScrollView實現(xiàn)qq側(cè)滑菜單
本文主要介紹了android自定義HorizontalScrollView實現(xiàn)qq側(cè)滑菜單的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android Socket接口實現(xiàn)即時通訊實例代碼
這篇文章主要介紹了Android Socket接口實現(xiàn)即時通訊實例代碼的相關(guān)資料,這里對通訊知識進行了詳細介紹,并用Socket 接口實現(xiàn)通訊實例,需要的朋友可以參考下2016-12-12Android studio 出現(xiàn)錯誤Run with --stacktrace option to get the s
這篇文章主要介紹了 Android studio 出現(xiàn)錯誤Run with --stacktrace option to get the stack trace. Run with --info or --debu的相關(guān)資料,需要的朋友可以參考下2016-11-11Android三方依賴沖突Gradle中exclude的使用
這篇文章主要介紹了Android三方依賴沖突Gradle中exclude的使用,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09進度條ProgressBar及ProgressDialog(實例)
下面小編就為大家?guī)硪黄M度條ProgressBar及ProgressDialog(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Android開發(fā)升級AGP7.0后的一些適配方法技巧
這篇文章主要為大家介紹了升級AGP7.0后的一些適配方法技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06