我想将图像添加到滚动视图,我尝试使用以下代码:
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
那么我应该如何将图像添加到我的滚动视图中?
添加代码: 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 调用此方法:
公共无效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 );
}
谢谢。