Scrollview只能托管一个直接的孩子[重复](Scrollview can host only one direct child [duplicate])

编程入门 行业动态 更新时间:2024-10-28 14:29:59
Scrollview只能托管一个直接的孩子[重复](Scrollview can host only one direct child [duplicate])

这个问题在这里已经有一个答案:

如何避免“IllegalStateException:Scrollview只能托管一个直接的孩子”? 4个答案

我有多个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>

This question already has an answer here:

How can I avoid “IllegalStateException: Scrollview can host only one direct child”? 7 answers

I have multiple LinearLayouts with a combined height that easily exceeds a device's screen height. So in order to make my layout scrollable, I tried adding in a ScrollView, but unfortunately I get the following error:

Scrollview can host only one direct child

I did some research, but couldn't really make sense of how to solve it, even after reading a couple of solutions. I tried a few changes, but nothing worked for me so far. Any help?

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

最满意答案

使用wrap_content将另一个LinearLayout中的所有孩子都包含在宽度和高度以及垂直方向上。

Wrap all the children inside of another LinearLayout with wrap_content for both the width and the height as well as the vertical orientation.

更多推荐

本文发布于:2023-08-07 18:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:孩子   host   Scrollview   duplicate   child

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!