Scrollview может содержать только один прямой дочерний элемент

У меня есть несколько LinearLayout с общей высотой, которая легко превышает высоту экрана устройства. Итак, чтобы сделать мой макет прокручиваемым, я попытался добавить ScrollView, но, к сожалению, получил следующую ошибку:

Scrollview может содержать только одного прямого дочернего элемента

Я провел некоторое исследование, но не мог понять, как его решить, даже прочитав пару решений. Я попробовал несколько изменений, но пока ничего не помогло мне. Любая помощь?

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="20dip">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 1"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q1_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 2"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q2_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 3"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q3_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 4"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q4_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 5"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q5_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 6"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q6_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 7"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q7_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 8"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q8_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 9"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q9_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 10"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q10_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 11"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q11_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 12"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q12_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 13"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q13_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 14"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q14_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 15"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q15_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 16"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q16_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 17"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q17_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 18"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q18_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 19"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q19_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 20"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q20_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Score:"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/final_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="0/5"
        android:gravity="center"/>
</LinearLayout>
    </LinearLayout>
        </ScrollView>

person user2407147    schedule 22.05.2013    source источник
comment
Решение: stackoverflow.com/a/55175944/6676310   -  person VIISHRUT MAVANII    schedule 01.02.2021
comment
Scrollview должен иметь только один дочерний элемент (ViewGroup), внутри которого вы можете добавить несколько представлений.   -  person Pravin Desai    schedule 21.05.2021


Ответы (1)


Оберните все дочерние элементы внутри другого LinearLayout с помощью wrap_content как для ширины, так и для высоты, а также для вертикальной ориентации.

person Justin Breitfeller    schedule 22.05.2013
comment
Это правильный путь. - person Marko Lazić; 22.05.2013
comment
Большое спасибо! Не могли бы вы взглянуть на еще один мой вопрос? ссылка - person user2407147; 22.05.2013
comment
Я ответил на ваш другой вопрос, не забудьте принять ответы с большим чеком, чтобы мы знали, что ответили на ваш вопрос. - person Justin Breitfeller; 22.05.2013
comment
Это также работает с Constraint Layout в настоящее время? - person maracuja-juice; 31.10.2017