2015-05-28 77 views
1

我试图把多个TextViews放在一个ScrollView以下的eachother中,但是当我这样做时,它会让我的应用程序崩溃。我怎么能在海誓山盟之下两次放置相同的文字?Android XML滚动视图

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

你可以发布你的完整的XML文件? –

+0

您只能在'ScrollView'中放置一个孩子,以便使用'LinearLayout'或任何其他容器 –

回答

3

按照机器人docmentaion: -

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

滚动型是一个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> 

我觉得上面的代码可以帮助你。

1

Scrollview可以只有一个孩子视图,这样就把布局和textviews这个布局里:

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

这是不对的,如果您将高度设置为match_parent以滚动视图,这将是unmeaning-full .. – DJphy

+0

是的,我现在可以滚动一百万英里而不需要任何内容​​。有任何解决这个问题的方法吗? – Markinson

+0

@Helix。现在有什么问题? –

1

滚动型的直接孩子应该是支持多个孩子另一个布局,如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> 
1

滚动视图只能有一个孩子。所以在你的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>