【问题标题】:ScrollView not Scrolling - AndroidScrollView 不滚动 - Android
【发布时间】:2015-05-17 01:57:13
【问题描述】:

我无法滚动我的滚动视图。它内部有一个 textView、一个 imageview 和一些线性布局。当我用 textview 替换 imageview 和线性布局时,它正在工作。这是我的代码:

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

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

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Drop Text Down"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="555dp"
            android:src="@drawable/ic_launcher" />

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ln"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#000000"
            android:orientation="vertical" >

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

                <TextView
                    android:id="@+id/TextView06"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView04"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

</LinearLayout>

【问题讨论】:

  • 尝试将 ScrollView 的 layout_height 更改为 wrap_content
  • @Bidhan A 我必须在滚动视图中使用布局权重。所以我不能将高度设置为 wrap_content
  • @David 你找到解决方案了吗?
  • 我发现它也令人沮丧,不仅如此.. 它的头脑如何滚动视图不能滚动:D 计算机怎么不能计算?只有一个答案:) 是的,我已将神奇的 layout_height 设置为神奇的值 wrap_content。然而,不是滚动。
  • 我想,当我在未执行的 ScrollView(在 android 模拟器中)上滚动鼠标滚轮时,它会滚动。瞧,在这之后,它确实对手指触摸滚动也有反应。哈哈哈,为什么 ?因为它是一个大杂烩。

标签: android android-layout imageview scrollview android-linearlayout


【解决方案1】:

放置一个固定高度的空视图

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

作为线性布局中的最后一项,它是滚动视图的子项。

这只是一个技巧,我不知道其中的逻辑。也许,在底部导航栏存在的情况下,滚动视图的行为是这样的。因此,如果我们添加另一个高度与底部导航栏高度(40-50)匹配的视图,它会表现良好。

【讨论】:

    【解决方案2】:

    ScrollView 的子视图应设置为 wrap_content。如果将其设置为 match_parent,它将填充ScrollView 的区域并且从不滚动,因为它不会大于ScrollView

    尝试将子 LinearLayout layout_height 更改为 wrap_content 或特定大小(以 dp 为单位)而不是 match_parent。

    【讨论】:

    • Nop 总是无济于事。这显然只是 ADT 的这个元素的几个问题之一......
    【解决方案3】:

    您的ScrollView 孩子的身高必须为wrap_content

    <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical"
        android:scrollbars="vertical" >
    
        ... 
        ...
    </LinearLayout>
    </ScrollView>
    

    【讨论】:

      【解决方案4】:

      我可以通过在活动清单中添加 (android:windowSoftInputMode="adjustResize|stateHidden") 来解决它,如下所示

      <activity
                  android:name=".ui.main.MainActivity"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustResize|stateHidden"
                  android:screenOrientation="portrait"
                  android:theme="@style/AppTheme.NoActionBar">
              </activity> 
      

      【讨论】:

        【解决方案5】:

        您应该将 LinearLayout(Scrollview 的子项)的高度设置为 wrap_content。

        当child高于ScrollView时,则android:fillViewport="true"属性无效。

        【讨论】:

        • 我知道。但我想为这些布局设置布局权重,所以我必须使用 fillViewPort。
        • @David 可能是你可以尝试 android:layout_height="wrap_content" 用于 id="imageView1" 的图像视图
        【解决方案6】:

        也许我为时已晚,但唯一能帮助我修复滚动视图在经过数小时的搜索和反复试验后无法滚动的方法是将滚动视图的高度设置为 0dp。

        这是一个示例 xml 代码供参考。

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <ScrollView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:fillViewport="true">
        <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
        
        </androidx.constraintlayout.widget.ConstraintLayout>
        </ScrollView>
        </androidx.constraintlayout.widget.ConstraintLayout>
        
        • 具有“match_parent”的顶部约束布局将适合屏幕。
        • 内部具有“0dp”的滚动视图会调整内部内容的高度,并在内容超出顶部/父约束布局时创建可滚动视图。
        • 最里面的约束布局(滚动视图的子视图)将包含视图和实际内容。

        【讨论】:

          【解决方案7】:

          如果将scrollview中的Linear layout的高度设置为match_parent,则如果高度与scrollview相同,则没有任何滚动。

          您必须将其设置为wrap-content。这样LinearLayout 的高度就会大于scrollview,然后你就可以滚动了。

          正确代码

          <ScrollView
              android:id="@+id/scrollView1"
              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:animateLayoutChanges="true"
                  android:orientation="vertical"
                  android:scrollbars="vertical" >
          

          附加说明: 您也不能在scrollview 中使用weightweightsum。这样做会使scrollview 无法滚动。

          【讨论】:

            【解决方案8】:

            使用表格布局而不是线性布局,如下所示:

            <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:fadeScrollbars="false" android:padding="6dip">
            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1"
                >   
               //your stuff 
            </TableLayout>
            </ScrollView>
            

            【讨论】:

            • 与线性布局或表格布局无关
            【解决方案9】:
            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true" >
            

            从上面的元素中移除 android:fillViewport="true"

            【讨论】:

            • 不,我有很多滚动视图,其中一些可以正常工作,有些则不能,一直在这个参数上。
            猜你喜欢
            • 1970-01-01
            • 2020-04-29
            • 2020-01-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-09
            相关资源
            最近更新 更多