当我像这样创建滚动视图时它不起作用为什么...?

when i created scroll view like this it's not working why...?

我创建了这样的滚动视图,但它不起作用,为什么我不知道请帮助 me.when 我确实这样做了,但在渲染过程中出现了类似 this:Exception 的错误:滚动视图可以主机只有一个直接 child.please 帮助我如何解决这个问题。 在此处输入代码

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

Scroll View can host only one direct child 错误本身说明了一切。

你在ScrollView里面拿了2个RelativeLayout,它必须最多一个child

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll

reference

如果您使用的是滚动视图,那么它只允许一个直接子视图。根据它的工作原理,它会滚动 space 超过可用数量的子项。 在您的场景中,相对布局不会占用太多 space 来支持滚动。它们也是您布局中的一些基本错误,例如永远不要在 sp.

中给出按钮宽度
<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">
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <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="300dp"
                      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>
    </LinearLayout>
 </ScrollView>
</LinearLayout>

希望对您有所帮助:)

滚动视图只能承载一个直接子视图

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

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