本文实例讲述了Android ScrollView只能添加一个子控件问题解决方法。分享给大家供大家参考,具体如下:
有下面一段代码
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
一个ScrollView里面添加了三个Button,也许你认为没有什么问题,那么我们运行一下看看
出现了一个异常

很明显,异常告诉我们ScrollView can host only one direct child
既然说只能容纳一个直接的子控件,那么我们就可以容纳多个间接的子控件,直接在这些子控件外面再套一层LinearLayout就OK了
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
希望本文所述对大家Android程序设计有所帮助。
本文介绍了在Android中ScrollView只能添加一个直接子控件的问题,并提供了解决方案。通过在外面包裹一层LinearLayout,可以成功地在ScrollView内添加多个Button等子控件,避免了运行时异常。这种方法对于需要在ScrollView中展示多个组件的情况非常实用。
8591

被折叠的 条评论
为什么被折叠?



