2015-06-04 43 views
16

我用新android.support.v4.widget.NestedScrollView和我面臨的問題。NestedScrollView和高度的WebView問題

這裏是我的佈局:

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="250dp"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:layout_collapseMode="parallax" 
      app:layout_collapseParallaxMultiplier="0.7"> 

      <!-- some views inside --> 

     </RelativeLayout> 

     <android.support.v7.widget.Toolbar 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:layout_collapseMode="pin" 
      /> 

    </android.support.design.widget.CollapsingToolbarLayout> 

    </android.support.design.widget.AppBarLayout> 

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

    <WebView 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</android.support.v4.widget.NestedScrollView> 

我需要加載HTML裏面的TextView,所以我提出:

content.setText(Html.fromHtml(htmlString));

而且這看起來很奇怪。我的textview放在屏幕的底部。 initial state

我滑過文本後,它開始看起來很正常。 after swipe

而且我覺得TextView的不僅是這些問題的看法。 我試過使用webview,但它甚至沒有顯示內容(我認爲由於不正確的高度計算)。 所以我需要的WebView或TextView的與NestedScrollView糾正工作。

P.S.如果我在dp中設置文本視圖高度,那麼文本看起來是正確的,但我需要wrap_content高度。

更新15年8月7日

最後,我需要使用WebView。亞歷克斯Facciorusso回答部分作品,但我面臨着另一個問題。當WebView內容具有某些特定高度時,我可以看到部分內容,但我無法向下滾動。 例子: enter image description here

+0

你可以嘗試webview顯示HTML .. ?????? – Moinkhan

+0

@Moinkhan yeap,但webview有另一個問題:它甚至沒有出現。而且我有很輕的html,所以'TextView'顯示它是正確的。 – IlyaEremin

+0

嘗試LinearLayout高度作爲match_parent ... – Moinkhan

回答

0

正如MAYUR Raiyani說:這個問題解決了,支持庫版本22.2.1:code.google.com/p/android/issues/detail?id=175234。感謝所有的答案。

+3

我正面臨類似的問題,@ IIyaEremin,它似乎沒有修復。 webview不尊重match_parents的高度。你確定這個問題是fixeD嗎? – onusopus

+0

@onusopus恕我直言,設計圖書館仍然是非常錯誤的,你需要使用一些第三方圖書館。 – IlyaEremin

+0

你也遇到過嗎? http://stackoverflow.com/questions/33688701/webview-height-grows-indefinitely-inside-a-nestedscrollview – onusopus

3

簡單的解決方法將是與500dp高度的TextView的下方添加您的LinearLayout內空視圖。該視圖應將您的TextView推到正確的位置。

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

<!-- Your scrolling content --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/random_text" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="500dp"/> 


</LinearLayout> 
1

我已經找到了解決辦法:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    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:fitsSystemWindows="true"> 

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

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:minHeight="300dp"> 

      <WebView 
       android:id="@+id/webview" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </FrameLayout> 

    </android.support.v4.widget.NestedScrollView> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="256dp" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsingToolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginEnd="64dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" /> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 
</android.support.design.widget.CoordinatorLayout> 

我加的500dp一到了minHeight NestedScrollView,現在的WebView是裝修佈局的所有高度,摺疊式工具正在工作。

已更新:用FrameLayout封裝WebView並將minHeight添加到它。

+0

Yeap,它的工作原理,但是有一些webview內容的高度,我看到部分內容,但我無法向下滾動。我試圖將minHeight更改爲1000dp,但沒有任何變化。 – IlyaEremin

+0

我更新了問題。 – IlyaEremin

+0

更新回答:也許它好一點 –

0

用的FrameLayout包裹web視圖和加入視圖(機器人:layout_height = 「800DP」)作爲WebView'minHeight給它。

-3

在NestedScrollView加入android:layout_gravity="fill_vertical"將解決您的問題

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

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="24dp" /> 

    </android.support.v4.widget.NestedScrollView> 
0

這是一個錯誤,更新你的SDK,將「編譯‘com.android.support:design:22.2.0’」在建「編譯‘com.android.support:design:22.2.1’」。 gradle這個。 這對我有用。

+0

嗨dhn!你知道是否有沒有與此相關的問題?謝謝! –

+0

對不起,這就是我所知道的。 –