【问题标题】:Android XML ScrollviewAndroid XML 滚动视图
【发布时间】:2015-08-10 16:35:11
【问题描述】:

我试图将多个TextViews 放在一个ScrollView 中,但是当我这样做时,它会使我的应用程序崩溃。如何将相同的文本放在彼此下方两次?

  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_below="@id/linear">

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/lorem_ipsum"/>

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/lorem_ipsum"/>
  </ScrollView> 

【问题讨论】:

  • 你能发布你的完整的xml文件吗?
  • 您只能将一个孩子放入ScrollView,因此请使用LinearLayout 或任何其他容器

标签: android xml textview scrollview lorem-ipsum


【解决方案1】:

你的Scrollview 可以有只有一个子视图,所以在这个布局中放置一个布局和你的文本视图:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_below="@id/linear">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum"/>

    </LinearLayout> 

</ScrollView> 

【讨论】:

  • 这是不对的,如果你将高度设置为 match_parent 以进行滚动视图,它将毫无意义。
  • 没错,我现在可以滚动一百万英里而没有任何内容。有什么办法解决这个问题?
  • @Helix。现在有什么问题?
  • 我可以在里面没有内容的情况下继续滚动和向下滚动。看起来它占用了最大的可滚动空间或其他东西。我里面有一小块 lorem ipsum,当我向下滚动并且它消失时,我可以继续滚动,而像 @DJphy 说的那样看不到任何东西
  • @RishiPaul 的回答似乎更有意义,为你竖起大拇指...试试那个螺旋
【解决方案2】:

ScrollView 的直接子级应该是另一个支持多个子级的布局,例如RelativeLayout 或LinearLayout。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum" />
    </LinearLayout>

</ScrollView>

【讨论】:

    【解决方案3】:

    滚动视图只能有一个孩子。所以在你的 ScrollView 中使用 LinearLayout,然后在里面做任何你想做的事情。像这样

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/app_name" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/app_name" />
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      根据 android 文档:-

      http://developer.android.com/reference/android/widget/ScrollView.html

      ScrollView 是 FrameLayout,这意味着您应该在其中放置一个包含要滚动的全部内容的子视图;这个孩子本身可能是一个具有复杂对象层次结构的布局管理器。经常使用的子元素是垂直方向的 LinearLayout,呈现用户可以滚动浏览的顶级项目的垂直数组。

      Scrollview 总是只包含一个子布局。并且线性布局具有方向属性来管理水平或垂直的子布局

      您的代码必须如下所示:

      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:orientation="vertical" >
               <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/lorem_ipsum"/>
      
               <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/lorem_ipsum"/>
         </LinearLayout></ScrollView>
      

      我认为上面的代码对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        • 2011-05-14
        • 1970-01-01
        相关资源
        最近更新 更多