【问题标题】:ScrollView not scrolling at allScrollView 根本不滚动
【发布时间】:2013-01-26 11:44:31
【问题描述】:

我无法使 ScrollView 正确滚动。它总是切断底部的内容,就好像它是一个普通的LinearLayout一样。

我的代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout android:id="@+id/scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="true"
        android:orientation="vertical" >

当然,我已经尝试添加/删除“fillViewport”和“isScrollContainer”属性,它并没有改变任何东西。

这适用于 ScrollView(仅限垂直)

Horizo​​ntalScrollView 必须用于水平滚动。

【问题讨论】:

  • 能否请您粘贴完整的 xml,可能与您的 LinearLayout 有关。
  • 尝试将android:layout_height="match_parent" 更改为android:layout_height="0dp" 并在ScrollView 上添加android:layout_weight = "1"
  • 刚刚试过,没有运气。
  • 约束布局的用户,请看link
  • 正如@SiddharthLele 建议的那样,将 layout_height 设置为 0dp 对我有用

标签: android scroll android-linearlayout android-scrollview


【解决方案1】:

Answer: ScrollView 在用作 XML 布局的根元素时不起作用。它必须被包裹在一个 LinearLayout 中。

然后解决:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <LinearLayout android:id="@+id/scroll_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>
</LinearLayout>

【讨论】:

  • 谢谢!无论 Android 多么愚蠢,我都不会想到这也可能是原因之一 -_-
  • 还需要将scrollView的layout_height更新为wrap_content
  • 这是不正确的,你不能使用ScrollView作为你的根元素,你可以使用ScrollView作为你XML的根元素,使用它作为根元素没有问题。
【解决方案2】:

❌尝试了上述所有答案,但我的问题仍未解决。 经过一些调试和更改后,我的问题得到了解决,即 ScrollView 正在滚动。

  • 我们不需要向 ScrollView 添加任何父元素以使其滚动(如此处的其他一些答案所述)。

✔️修复是针对我的问题✔️

  • 将 ScrollView 的高度改为 0dp

    android:layout_height="0dp"

  • 将顶部和底部限制为父/各自的视图/元素

    app:layout_constraintTop_toBottomOf="@+id/imageView4" app:layout_constraintBottom_toBottomOf="parent"

最终的 xml 看起来像这样

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/imageView4"
    app:layout_constraintBottom_toBottomOf="parent">

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

           <!-- Loong content -->

    </LinearLayout>
</ScrollView>

【讨论】:

    【解决方案3】:

    Scroll View as Parent 没有问题。当我们将填充/边距添加到滚动视图的直接子级时,我遇到了这样的问题。让滚动视图的子视图只保留高度和宽度属性,这样它就可以正常工作了。

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:fillViewport="true">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:animateLayoutChanges="true"
                android:orientation="vertical">
    </LinearLayout>
    </ScrollView>
    

    【讨论】:

    • 天啊,我已经为此苦苦挣扎了一个小时。这是黄金。
    • 也为我工作!谢谢!
    【解决方案4】:

    所选答案不正确!

    您可以使用 ScrollView 作为根视图,它对您不起作用,因为您缺少填充。

    添加类似:

    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    

    【讨论】:

    • 如何知道多少填充就足够了?我的意思是,由于所有设备都不同,这不会引起问题吗?
    【解决方案5】:

    删除LinearLayout 中的android:isScrollContainer。根据文档android:isScrollContainer 用于设置可滚动的视图。我希望它对你有帮助。请参阅此link 了解定义。

    【讨论】:

    • 谢谢。谢谢你。谢谢。
    【解决方案6】:

    Android Studio 将 NestedScrollView 添加到其某些模板(例如 Master-Detail)的活动文件中。在片段文件中拥有一个 ScrollView 并且在该片段的活动文件中拥有另一个会阻止滚动视图工作。删除片段文件中的 ScrollView 并将其保留在活动文件中解决了我的问题。

    【讨论】:

      【解决方案7】:

      试试这个解决方案,只是使用延迟后的方法来延迟滚动。

      fragment_test.xml

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/rootRelativeLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          xmlns:fresco="http://schemas.android.com/apk/res-auto">
      
          <ScrollView
              android:id="@+id/scrollView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop="40dp"
              android:fillViewport="true"
              android:scrollbars="none">
      
              <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
                  ...
              </RelativeLayout>
      
          </ScrollView>
      
      </RelativeLayout>
      

      TestFragment.java

      ...
      private ScrollView mScrollView;
      ...
      mScrollView = (ScrollView) mView.findViewById(R.id.scrollView);
      mScrollView.setSmoothScrollingEnabled(true);
      ...
      new Handler().postDelayed(new Runnable() {
          @Override
           public void run() {
               if(isAdded()) {
                   // Scroll 1000px down
                   mScrollView.smoothScrollTo(0, 1000);
               }
           }
      }, 150);
      

      这对我有用,希望这会有所帮助。

      【讨论】:

        【解决方案8】:

        试试这个,

        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:fillViewport="true" >
        
        <LinearLayout android:id="@+id/scroll_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        

        【讨论】:

          【解决方案9】:

          我终于发现我花了 5 个小时逐步构建所有内容并一次测试每一步,呃......

          无论如何,如果您对此有疑问,我发现 setOntouch 侦听器正在弄乱您的代码 - 至少在我的情况下是...

          我必须想办法解决它,但尝试删除你的 ontouch 监听器并测试你的代码 - 它必须工作

          【讨论】:

          • 谢谢你,工作...
          【解决方案10】:

          这解决了我的问题:我将 android:isScrollContainer="true" 添加到 LinearLayout 并删除了 ScrollView 之前的代码:

              <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          <ScrollView
              android:id="@+id/scrollViewRecords"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:fillViewport="true">
          
              <LinearLayout
                  android:id="@+id/sheepList"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" >
          
              </LinearLayout>
          </ScrollView>
          </LinearLayout>
          

          之后的代码

              <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:isScrollContainer="true">
          
              <LinearLayout
                  android:id="@+id/sheepList"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" >
          
              </LinearLayout>
          </LinearLayout>
          

          【讨论】:

            【解决方案11】:

            有没有办法结束这个问题?它很旧,目前标记为有效的答案没有意义。现在关于 ScrollView 唯一有效的是:

            允许滚动放置在其中的视图层次结构的视图组。 滚动视图中可能只有一个直接子级。要在滚动视图中添加多个视图,请让直接子视图添加一个视图组,例如 LinearLayout,并在该 LinearLayout 中放置其他视图。 永远不要将 RecyclerView 或 ListView 添加到滚动视图。这样做会导致糟糕的用户界面性能和糟糕的用户体验。

            取自官方文档:https://developer.android.com/reference/android/widget/ScrollView.html

            【讨论】:

              【解决方案12】:
                  <LinearLayout 
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">
                  
                      <ScrollView
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:fillViewport="true">
                  
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:orientation="vertical">
              ......
              </LinearLayout>
              </ScrollView>
              </LinearLayout>
              

              【讨论】:

              • 不要在布局中添加任何边距和内边距
              • 接受的答案已经回答了问题。还应添加结束 xml 标记 - 这可能会被误认为是父布局内的 2 个布局。同时添加一些描述。
              【解决方案13】:

              看起来您没有使用某些属性和规则。

              android:layout_height="0dp"
              android:scrollbars="none"
              android:fillViewport="true"
              

              此外,在此之后尝试将所有滚动内容包装在约束布局中。

              <tech.loung.views.SlidingScrollView
              android:id="@+id/scrollview_settings"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:scrollbars="none"
              android:fillViewport="true"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/top_layout">
              

              【讨论】:

                【解决方案14】:

                在我的场景中,当我使用 Padding=10dp 更新滚动视图内的线性布局时,它起作用了。就是这样滚动视图滚动成功

                <ScrollView
                    android:id="@+id/mainScrollview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="?attr/actionBarSize">
                
                
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/top_layout"
                        android:orientation="vertical"
                        android:layout_margin="10dp"
                        android:padding="10dp">
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-12-03
                  • 2020-04-13
                  • 2020-03-19
                  • 1970-01-01
                  相关资源
                  最近更新 更多