9

這是我的佈局。當我加載google.com時,webview的高度會無限增長。 webview的onSizeChange函數不斷被調用,我可以看到webview不斷擴展。我試過了22.2.1和23.1.0的設計和appcompat庫,沒有任何效果。webview高度在nestedScrollView中無限增長

任何解決方案? : - |

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/black"> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="fill_vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <com.example.ajay.scrollabletoolbar.MyWebView 
     android:id="@+id/webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true"/> 
</android.support.v4.widget.NestedScrollView> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbarlayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/restricted_browser_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?actionBarSize" 
     android:background="@color/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#FFFFFF" 
      android:descendantFocusability="beforeDescendants" 
      android:focusableInTouchMode="true"> 

      <EditText 
       android:id="@+id/current_url" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_centerVertical="true" 
       android:backgroundTint="@android:color/darker_gray" 
       android:dropDownAnchor="@+id/restricted_browser_toolbar" 
       android:hint="hint hint" 
       android:imeOptions="actionGo|flagNoExtractUi" 
       android:inputType="textNoSuggestions|textUri" 
       android:paddingBottom="8dp" 
       android:paddingEnd="8dp" 
       android:paddingStart="8dp" 
       android:singleLine="true"/> 

      <ImageView 
       android:id="@+id/clear_url_text" 
       android:layout_width="48dp" 
       android:layout_height="48dp" 
       android:layout_alignRight="@+id/current_url" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="8dp" 
       android:src="@android:drawable/ic_menu_close_clear_cancel" 
       android:visibility="gone"/> 
     </RelativeLayout> 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

+0

將其設置爲固定大小 – Nanoc

+1

我不認爲我可以使用固定大小。我希望webview能夠在所有手機和平板電腦中佔據整個窗口。 – onusopus

+0

當然,您可以像爲webview設置窗口大小一樣簡單,爲什麼你認爲你不能? – Nanoc

回答

5

在一個句子,你只是無法把你WebViewNestedScrollView這是按預期工作的。

如果你把WebViewNestedScrollView(或ScrollView),您WebView措施與View.MeasureSpec.UNSPECIFIED要求的尺寸。無論你設置MATCH_PARENT甚至固定的大小像你的WebView的高度100dp。 NestedScrollView強迫其子女用View.MeasureSpec.UNSPECIFIED要求自己測量。這一切發生在NestedScrollViewmeasureChild()方法。它是這樣如下:

@Override 
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) { 
    ViewGroup.LayoutParams lp = child.getLayoutParams(); 
    int childWidthMeasureSpec; 
    int childHeightMeasureSpec; 
    childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, getPaddingLeft() 
      + getPaddingRight(), lp.width); 
    childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
    child.measure(childWidthMeasureSpec, childHeightMeasureSpec); 
} 

而且MeasureSpec.UNSPECIFIED手段,其標籤所暗示的,也可以是它想要的任何大小。我認爲從"Writing Custom Views for Android" session at Google I/O 2013這張幻燈片將幫助你理解它。

enter image description here

其實這是這是在this thread討論的問題,並根據this Google engineer

這是因爲如果在桌面上,你調整瀏覽器窗口,所以它的頁面的高度不能垂直滾動。滾動發生在NestedScrollView而不是webview本身。因此,頁面中沒有js的結果會看到任何滾動事件,因此它不知道您已滾動到頁面末尾以加載更多內容。

因此,底線是,不要把你WebViewNestedScrollView。您應該讓WebView滾動網頁本身。快樂的編碼! :)

0

我很抱歉未提前關閉此問題。我們無法在nestedScrollView中放置一個webview。但是NSV的本質是,它將孩子擴展到其全部高度,這樣它就可以按照預期的方式使用工具欄來獲取可摺疊/可滾動的工具欄等。因此,如果添加了webview,則webview將無限增長或增大價值,因此它不會按預期工作。這可以是一個ref:code.google.com/p/android/issues/detail?id=198965