灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2393回复:0

Android中ScrollView只能添加一个子控件

楼主#
更多 发布于:2012-09-06 13:59


有下面一段代码
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical" >

    <ScrollView
        Android:layout_width="fill_parent"
        Android:layout_height="fill_parent" >

        <Button
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content" />

        <Button
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content" />

        <Button
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical" >
    <ScrollView
        Android:layout_width="fill_parent"
        Android:layout_height="fill_parent" >
        <Button
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content" />
        <Button
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content" />
        <Button
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content" />
    </ScrollView>
</LinearLayout>一个ScrollView里面添加了三个Button,也许你认为没有什么问题,那么我们运行一下看看
出现了一个异常


很明显,异常告诉我们ScrollView can host only one direct child
既然说只能容纳一个直接的子控件,那么我们就可以容纳多个间接的子控件,直接在这些子控件外面再套一层LinearLayout就OK了
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical" >

    <ScrollView
        Android:layout_width="fill_parent"
        Android:layout_height="fill_parent" >

        <LinearLayout
            Android:layout_width="fill_parent"
            Android:layout_height="fill_parent"
            Android:orientation="vertical" >

            <Button
                Android:layout_width="fill_parent"
                Android:layout_height="wrap_content" />

            <Button
                Android:layout_width="fill_parent"
                Android:layout_height="wrap_content" />

            <Button
                Android:layout_width="fill_parent"
                Android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>


喜欢0 评分0
游客

返回顶部