久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Android Webview滑進出屏幕閃爍的解決方法

這篇文章主要給大家介紹了關于Android Webview滑進出屏幕閃爍的解決方法,文中通過示例代碼介紹的非常詳細,對各位Android開發者們具有一定的參考學習價值,需要的朋友們下面來一起學

前言

在使用Webview進行滑動操作時,從屏幕可見區域外向內滑動時,會出現webview區域閃爍的問題(反之也是),本文將提供一種解決方案。

問題圖示

xml布局:


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:fillViewport="true"
 android:overScrollMode="never"
 android:scrollbars="none">

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <View
 android:id="@+id/contentView"
 android:layout_width="match_parent"
 android:layout_height="600dp"
 android:background="@color/colorPrimary" />
 <WebView
 android:id="@+id/webView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@color/contract_font"></WebView>
 </LinearLayout>
</android.support.v4.widget.NestedScrollView>

可以看到,NestedScrollView嵌套webview,且webview初始未在一屏內時,滑進出屏幕時會有短暫的白色塊。

解決問題

方案對比

方案 考慮點
android:hardwareAccelerated="false" 5.0 開始Android系統為了充分利用GPU的特性,使得界面渲染更加平滑而默認開啟的,如果關掉的話,那么整個網頁不流暢了,豈不是得不償失——>放棄
setBackgroundColor(Color.parseColor(“#00000000”)); setBackgroundResource(R.drawable.white); 設置底色背景,但是webview本身是加載的H5頁面,使用的是H5頁面的底色背景,而且通過上面的gif可以看出,沒有效果——>放棄
==通過樣式布局,讓webview保持在第一屏內初始化== 本文嘗試的方案

方案探索

1.xml布局


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:fillViewport="true"
 android:overScrollMode="never"
 android:scrollbars="none">

 <FrameLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <WebView
 android:id="@+id/webView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@color/contract_font"></WebView>

 <View
 android:id="@+id/contentView"
 android:layout_width="match_parent"
 android:layout_height="600dp"
 android:background="@color/colorPrimary" />
 </FrameLayout>
</android.support.v4.widget.NestedScrollView>

通過FrameLayout來疊加使得webview保持在第一屏內初始化,然后設置webview的padding,這樣使得完整的H5內容是在ContentView下方顯示。

但是——>webview設置padding根本無效!!!

怎么辦呢?無論怎樣也想不到為什么會如此,畢竟本身api的實現上是有些缺陷的(https://stackoverflow.com/questions/9170042/how-to-add-padding-around-a-webview )

2.解決問題

最終的解決方案則是通過注入js代碼來控制H5的padding來解決。


 webView.setWebViewClient(new WebViewClient() {
 @Override
 public void onPageFinished(WebView view, String url) {
 contentView.post(new Runnable() {
  @Override
  public void run() {
  contentViewHeight = px2dp(getApplicationContext(), contentView.getMeasuredHeight());
  if (contentViewHeight > 0) {
  webView.loadUrl("javascript:document.body.style.marginTop=\"" + contentViewHeight + "px\"; void 0");
  }
  }
 });
 }
 });

看下猜想運行的結果:

H5的顯示缺少了頂部,這樣看來padding是沒有效果的。但是,為什么會沒有效果呢,難道設置padding有問題?
之后查看了上面嵌入的網頁的源碼查看了下(網頁是網絡上隨便找的一個url):

https://36kr.com/

打開網頁編輯模式,查看body這塊的樣式:

可以看到要注入的js控制的樣式這塊是沒有設置的。因此可以將padding-top的參數通過這里設置進去。

但是發現設置的該參數無效,是什么原因呢?接著往下翻:

原來是body中控制了padding-top的最高級樣式顯示,所以element-style中設置無效。所以要么把這段注釋掉,重新寫入至element-style中,要么嘗試設置margin-top的方法。這里采用后者的做法:

可以看到,網頁頂部出現了設置好的marin-top空白的高度。

只需要將這部分操作轉換為對應的代碼即可:

將上面的


webView.loadUrl("javascript:document.body.style.paddingTop="" + contentViewHeight + "px"; void 0");

替換為:


webView.loadUrl("javascript:document.body.style.marginTop=\"" + contentViewHeight + "px\"; void 0");

3.運行效果

可以看到已經沒有閃爍了。

總結

整個方案的實現其實就兩塊:

1.布局,讓webview在一屏內初始;

2.設置H5網頁的margin-top或者padding-top;

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對html5模板網的支持。

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

這篇文章主要介紹了Android開發之TabHost選項卡及相關疑難解決方法,結合實例形式較為詳細的分析了Android開發中TabHost選項卡的常見用法以及相關疑難問題解決方法,需要的朋友可以參考下
這篇文章主要介紹了Android TabHost選項卡標簽圖標始終不出現的解決方法,涉及Android界面布局相關屬性與狀態設置操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之Notification手機狀態欄通知用法,結合實例形式分析了Android Notification手機狀態欄通知的常見函數、功能及使用技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發實現模仿微信小窗口功能,結合實例形式分析了Android實現微信風格Dialog對話框窗口相關功能與布局操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之PopupWindow創建彈窗、對話框的方法,結合實例形式詳細分析了Android使用PopupWindow創建對話框相關操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之DatePickerDialog、TimePickerDialog時間日期對話框用法,結合實例形式分析了Android使用DatePickerDialog、TimePickerDialog顯示日期時間相關操作技巧,需要的朋友可以參考
主站蜘蛛池模板: 欧美淫 | 亚洲在线| 日本在线中文 | 国产精品福利在线 | aaa在线| 精品免费av | 亚洲天堂精品久久 | 亚洲免费视频一区 | 成人黄色在线 | 一区二区三区欧美在线 | 欧美日韩久久久 | 一级片毛片 | 日韩国产精品一区二区三区 | 一级电影免费看 | 成人欧美一区二区三区白人 | 国产精品视频在 | 国产高清精品一区二区三区 | 成人在线免费观看 | 国产精品99久久久久 | 欧美xxxx性| 性xxxxx| 欧美激情一区二区三区 | 国产在线二区 | 亚洲高清一区二区三区 | 一区二区精品电影 | 欧美一区2区三区4区公司二百 | 免费人成在线观看网站 | 国产精品永久免费观看 | 欧美一级免费观看 | 国产精品久久毛片av大全日韩 | 91精品国产综合久久福利软件 | 国产精品五区 | 久久免费精品 | 最近最新中文字幕 | 精品国产91 | 国产激情毛片 | 在线观看av网站 | 免费爱爱视频 | 精品国产一区二区在线 | 伊人网综合 | 丝袜一区二区三区 |