当我创建这样的滚动视图时,它不工作为什么......?

时间:2016-06-16 09:09:36

标签: android scrollview android-linearlayout relativelayout

我创建了这样的滚动视图,但它不起作用为什么我不知道请帮助我。当我这样做时,我得到这样的错误:渲染期间出现异常:Scroll View只能托管一个直接的孩子。请帮助我如何解决这个问题。         在这里输入代码

<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"
android:orientation="vertical"
tools:context="com.example.jeshtamsru.tntrains.DashboarsActivity">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal">
    <Button
        android:layout_width="wrap_content"
        android:width="300sp"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="chennai to coimbatore"/>
</RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <Button
            android:layout_width="wrap_content"
            android:width="300sp"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:text="chennai to coimbatore"/>
    </RelativeLayout>
</ScrollView>

3 个答案:

答案 0 :(得分:1)

如果您使用滚动视图,那么它只允许一个直接子项。并且根据它的工作原理,它会滚动孩子,这个孩子的空间比可用空间大。 在您的场景中,相对布局不占用太多空间来支持滚动。而且它们在你的布局中也存在一些基本错误,比如从不在sp中给出按钮宽度。

function createMyRecord (data){
  let record = new MyRecord(data)

  return record.set('key', Math.random().toString())
}

希望它会有所帮助:)

答案 1 :(得分:0)

滚动视图只能托管一个直接孩子错误本身就说明了一切。

RelativeLayout内{2} ScrollView最多只有一个孩子

  

ScrollView是一个FrameLayout,意味着你应该放置一个孩子   它包含要滚动的全部内容

reference

答案 2 :(得分:0)

滚动视图只能托管一个直接孩子

将滚动视图中的布局包含在相对或线性布局中。

<ScrollView
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
   <RelativeLayout
      ... (stuff you had inside your ScrollView)
   </RelativeLayout>
 </ScrollView>