android - ScrollView 只能承载一个直接子异常

标签 android

我想将图像添加到 ScrollView ,有我尝试使用的代码:

        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: ScrollView can host only one direct child

那么我应该如何将图像添加到我的 ScrollView 中呢?

添加代码: 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>

最后 Activity onCreate 调用这个方法:

public void addImage(字符串路径){

            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 ); 

}

谢谢。

最佳答案

这可能有帮助..它告诉我们是因为 ScrollView 不能容纳超过 1 个 child ..它需要一个 child 来承载所有其他 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 ); 

关于android - ScrollView 只能承载一个直接子异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316235/

相关文章:

android - 无法从移动设备浏览本地计算机上托管的网站

android - 未调用来自 GCMIntentService 的 onRegistered()

android - 使用 onBind 方法接收命令

android - 文本未出现在 ListView 中

android - 在 ProgressBar 中显示数字

Android IntentService 和状态

android - Genymotion 卡在黑屏

java - 在服务接收器中使用什么来实例化对象的正确上下文是什么?

Android - 更改操作栏标题文本颜色

android - 强制停止后如何保持服务运行?