滚动型只能承载一个直接子例外 [英] ScrollView can host only one direct child exception

查看:133
本文介绍了滚动型只能承载一个直接子例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加图像,滚动型,有code我试图用

I want to add images to scrollview, there is code i tried to use:

        ScrollView sv = (ScrollView)findViewById( R.id.scrollView2);
        ImageView iv = new ImageView(this);
        iv.setImageDrawable( new BitmapDrawable( "PATH" ) );
        iv.setScaleType( ScaleType.CENTER_INSIDE );
        sv.addView( sv ); 

我得到这个异​​常:
java.lang.IllegalStateException:滚动型只能承载一个直接子

I get this exception: java.lang.IllegalStateException: ScrollView can host only one direct child

因此​​,如何,我应该将图像添加到我的滚动视图?

So how i should add images to my scrollview ?

新增code:
XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#FF0000" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        </LinearLayout>
    </ScrollView>

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/scrollView1"
        android:background="#FFFF00" >

        <LinearLayout
            android:id="@+id/filesScrollerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </LinearLayout>
    </ScrollView>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="#FFFFFF"
        android:layout_toRightOf="@+id/scrollView2" />

</RelativeLayout>

和上年末活动的onCreate调用此方法:

And on the end Activity onCreate calling this method:

公共无效addImage(字符串路径){

public void addImage(String path){

            LinearLayout sv = (LinearLayout)findViewById( R.id.filesScrollerLayout);
        ImageView iv = new ImageView(this);
        iv.setImageDrawable( new BitmapDrawable( path ) ); 
        iv.setScaleType( ScaleType.CENTER_INSIDE );
        sv.addView( sv ); 

}

感谢。

推荐答案

这可以帮助..它告诉如此,因为LinearLayout中不能持有超过1个小孩。它需要哪些主机的所有其它视图一个孩子。

this may help.. it tells so because linearLayout cannot hold more than 1 child.. it needs single child which hosts all other view..

<ScrollView>
<LinearLayout
android:id="@+id/child">
    <ImageView/>
    ...
    ...
</LinearLayout>
</ScrollView>

在你的情况

LinearLayout child = (LinearLayout)findViewById( R.id.child);
        ImageView iv = new ImageView(this);
        iv.setImageDrawable( new BitmapDrawable( "PATH" ) );
        iv.setScaleType( ScaleType.CENTER_INSIDE );
        child.addView( sv ); 

这篇关于滚动型只能承载一个直接子例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆