【问题标题】:ScrollView doesn't work in Android LayoutScrollView 在 Android 布局中不起作用
【发布时间】:2014-01-13 12:03:20
【问题描述】:

我为我的测试应用程序设计了一个简单的布局,一个 TextView,然后是一个 ImageView 和一个 TextView。但是第二个 TextView 我希望它可以滚动(通过触摸)。

我在网上搜索并尝试了很多解决方案,但仍然无法成功。

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:scrollbars="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/title"
        android:textSize="40sp"
        android:textStyle="bold" />


        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:contentDescription="@string/ada_lovelace"
        android:paddingTop="8dp"
        android:src="@drawable/ada_lovelace" />

    <ScrollView 
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:id="@+id/scroll1"
        android:layout_below="@id/imageView1">  

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:text="@string/hello_world"
            android:textSize="22sp" />


    </ScrollView>


</RelativeLayout>

请帮帮我。

【问题讨论】:

  • 只有在 textview 有更多元素需要滚动时才有效
  • 参考这个链接,解决了我的问题....stackoverflow.com/questions/1748977/…..
  • 使用 ListView 和简易适配器
  • 请检查在第二个文本视图中添加更多内容
  • ScrollView 仅在内部布局超过 ScrollView 本身的大小时才有效。让我举个例子。假设您有一个具有 100dp 高度和 fill_parent 宽度的 ScrollView。它有一个 TextView,它有 150dp 的高度和 wrap_content 的宽度。由于 ScrollView 的高度为 100dp,因此您的 TextView 将可垂直滚动 50dp。所以在你的例子中,因为你的话,可能默认为“Hello world!”不超过您的 ScrollView 大小(注意您的 ScrollView 高度是屏幕大小),它不会滚动。

标签: android android-layout android-relativelayout android-xml


【解决方案1】:

您可以使用this 链接。而在xml中使用scrollview的格式如下:

  <linearlayout>
      <scrollview>
        <linear>or <relativelayout>
        </linear layout>
      </scrollview>
  </linear layout>.

【讨论】:

    【解决方案2】:

    仅当其子项的一部分由于尺寸或大数字而在屏幕上不可见时,滚动才有效

    【讨论】:

      【解决方案3】:

      几周前我遇到了同样的问题,这是我的解决方案:

      <ScrollView
          android:id="@+id/scrollView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
      
          <RelativeLayout
              android:id="@+id/RelativeLayout2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical" >
      
              <TextView
                  android:id="@+id/textview"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/textview" />
          </RelativeLayout>
      </ScrollView>
      

      但如果 TextView 中没有“足够”的内容,则它是不可滚动的。只有当它比你的屏幕更“捕获”时。

      【讨论】:

      • 仍然不适合我。 hello_world 字符串我已更改为某个段落,它的长度足以滚动但它不会移动:(
      【解决方案4】:

      重新导入项目后一切正常。我不知道这是什么错误,但可能是因为 eclipse 故障。

      【讨论】:

        【解决方案5】:

        您可以使用以下代码使 TextView 可滚动,而不是为您的 TextView 使用 ScrollView。

        <TextView
            android:id="@+id/txtView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="5"
            android:scrollbars="vertical"
            android:textAppearance="?android:attr/textAppearanceSmall" />
        

        在你的 Java 文件中使用这个代码

        txtView.setMovementMethod(new ScrollingMovementMethod());
        

        希望这会有所帮助。

        【讨论】:

          【解决方案6】:

          TextView 不需要 ScrollViewTextView 具有 scrolling 属性。

          尝试设置一下,看看是否有效。

          【讨论】:

            【解决方案7】:
            1. 将 ScrollView 的高度设置为 38dp。
            2. 将 TextView 的大小设置为 40sp。

            然后你可以滚动TextView。

            【讨论】:

              【解决方案8】:

              Scrollable 只是 ScrollView 之间的部分

              因此,如果您想滚动所有内容(正如我所理解的那样),您只需要更改顺序即可。 我也认为 ScrollView 需要在布局中并有布局,因此您需要两种布局:一种内部和一种外部。虽然我不确定最后一句话 - 我才刚刚开始学习东西

              【讨论】:

                【解决方案9】:

                这是实现此代码并运行代码 textview 可滚动的解决方案。

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/title"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:contentDescription="@string/ada_lovelace"
                    android:paddingTop="8dp"
                    android:src="@drawable/ada_lovelace" />
                
                <ScrollView
                    android:layout_height="fill_parent"
                    android:layout_width="wrap_content"
                    android:id="@+id/scroll1"
                    android:layout_below="@id/imageView1">
                
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                
                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="8dp"
                        android:text="@string/hello_world"
                        android:textSize="22sp" />
                
                    </RelativeLayout>
                </ScrollView>
                

                【讨论】:

                  【解决方案10】:

                  应该可以试试

                  <ScrollView
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content" 
                  android:padding_bottom = "10dp">
                  <TextView
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="@string/textview" />
                  </ScrollView>
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-01-20
                    • 1970-01-01
                    • 2018-01-21
                    • 2020-03-13
                    • 1970-01-01
                    • 2017-08-28
                    相关资源
                    最近更新 更多