【问题标题】:NestedScrollView and WebView height issueNestedScrollView 和 WebView 高度问题
【发布时间】:2015-06-04 11:49:57
【问题描述】:

我使用新的android.support.v4.widget.NestedScrollView,但遇到了问题。

这是我的布局:

<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>

我需要在textView中加载html,所以我做:

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

而且看起来很奇怪。我的 textview 放在屏幕底部。

在我滑动文本后,它开始看起来很正常。

而且我认为 textview 不仅仅是这些问题的视图。 我尝试使用 webview,但它甚至不显示内容(我认为是由于高度计算不正确)。 所以我需要 webview 或 textview 来纠正 NestedScrollView 的工作。

附:如果我在 dp 中设置 textview 高度,那么文本看起来正确,但我需要 wrap_content 的高度。

08.07.15 更新

最后我需要使用WebView。 Alex Facciorusso 的回答部分有效,但我遇到了另一个问题。当WebView 内容具有特定高度时,我可以看到部分内容,但无法向下滚动。 例子:

【问题讨论】:

  • 你可以尝试使用 webview 来显示 HTML ..??????
  • @Moinkhan 是的,但是 webview 有另一个问题:它甚至没有出现。而且我的 html 很轻,所以TextView 显示它是正确的。
  • 尝试将 LinearLayout 高度作为 match_parent...
  • @Moinkhan 我尝试了很多配置:LinearLayout 设置为match_parentRelativeLayoutmatch_parentwrap_content,将TextView 直接放在NestedScrollView 中。我认为这是一个NestedScrollView 问题,但我不知道出了什么问题。
  • 在支持库版本 2.2.1 中解决了这个问题:code.google.com/p/android/issues/detail?id=175234

标签: android android-design-library androiddesignsupport


【解决方案1】:

阅读了很多帖子,也阅读了 WebView 的自定义实现,但我热衷于尝试属性并发现什么对我有用,这就是:

对于NestedScrollView 使用属性

android:fillViewport="true"

对于底层WebView,请确保您使用高度作为包装

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

所以你有一个伪代码

<NestedScrollView> // height=  match_parent

 <Parent_layout>//could be Realtive/Linear with height= match_parent
   <Some_View></Some_View>
   <Some_View></Some_View>

   <WebView></WebView> // height=  wrap_content
 </Parent_layout>

</NestedScrollView>

【讨论】:

  • 这解决了webview放在nestedscrollview中时无限滚动的问题!好一个!
  • 哦,太好了@ArunJose,这对你有用!这个问题每年都会提醒我一次解决方案哈哈哈。
【解决方案2】:

一个简单的解决方法是在 LinearLayout 内的 TextView 下方添加一个高度为 500dp 的空视图。该视图应将您的 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>

【讨论】:

    【解决方案3】:

    我找到了解决方法:

    <?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>
    

    我在 NestedScrollView 中添加了一个 500dp 的 minHeight,现在 WebView 适合布局的所有高度,并且折叠工具栏正在工作。

    更新:用 FrameLayout 包装 WebView 并为其添加 minHeight。

    【讨论】:

    • 是的,它可以工作,但是对于一些 webview 内容高度,我可以看到部分内容,但我无法向下滚动。我尝试将 minHeight 更改为 1000dp,但没有任何改变。
    • 更新答案:也许会好一点
    • 抱歉迟到了。我试图将WebView 包装成FrameLayout。结果是一样的。你可以自己试试。这是我的 webView 的 xml 和数据:gist.github.com/IlyaEremin/e28d51cd1341607a1e69
    • 我想提一下将FrameLayout 中的WebViewminHeight 包装在一起帮助我解决了一个与在触摸webview 的内容进行滚动时无法滚动WebView 相关的问题。
    【解决方案4】:

    正如 Mayur Ra​​yani 所说:此问题已在支持库版本 22.2.1 中得到解决:code.google.com/p/android/issues/detail?id=175234。谢谢大家的回答。

    【讨论】:

    • 我面临着类似的问题,@IIyaEremin,它似乎没有得到解决。 webview 不尊重 match_parents 的高度。您确定问题已解决吗?
    • @onusopus 恕我直言设计库仍然有很多问题,您需要使用一些 3rd 方库。
    • 你也遇到过这种情况吗? stackoverflow.com/questions/33688701/…
    • 哇,谢谢。问题是,有没有天使从天而降,神仙派来,在你耳边低语说有22.2.1这样的版本?我认为有一个列表,我正在努力寻找支持库版本的列表...
    • @NeonWarge 使用 Android SDK 管理器检查更新的版本,并使用支持库更改日志了解更改的概述developer.android.com/tools/support-library/index.html
    【解决方案5】:

    用 FrameLayout 包装 WebView 并将 View(android:layout_height="800dp") 作为 WebView'minHeight 添加到它。

    【讨论】:

      【解决方案6】:

      这是一个错误,更新你的 sdk,将 build.gradle 中的 "compile 'com.android.support:design:22.2.0'" 替换为 "compile 'com.android.support:design:22.2.1'" . 这对我有用。

      【讨论】:

      • 您好 dhn!您知道是否存在与此无关的问题吗?谢谢!
      • 抱歉,我知道的就这些。
      【解决方案7】:

      在 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>
      

      【讨论】:

        猜你喜欢
        • 2015-12-07
        • 2016-02-14
        • 2013-03-13
        • 2011-04-30
        • 1970-01-01
        • 2021-04-26
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        相关资源
        最近更新 更多