如何动态地向ScrollView添加图像?

时间:2012-04-25 13:42:45

标签: android

我正在尝试动态地将图像添加到scrollview。要将图像添加到scrollview,我使用此代码:

        LinearLayout sv = (LinearLayout)findViewById( R.id.filesScrollerLayout);
        ImageView iv = new ImageView(this);
        iv.setImageDrawable( new BitmapDrawable( pub.FirstPicture ) ); // same happens with ScaleDrawable.
        iv.setScaleType( ScaleType.CENTER_INSIDE );
        sv.addView( iv ); 


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

所以我需要像这样在这个滚动视图中添加图像:

|-------------
|
|Image 1
|-------------
|
|Image 2
|-------------
|
|Image 3
|-------------

为此,我需要以某种方式更改Scrollview的内容大小?

如何在Android中完成此操作?

感谢。

2 个答案:

答案 0 :(得分:3)

<LinearLayout
        android:id="@+id/filesScrollerLayout"
        android:orientation="vertical"     << this line
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>


LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
layout .addView(imageView);

答案 1 :(得分:0)

你能告诉我们现在发生了什么吗?它是否正在运行且没有任何显示?

我有一种预感,你必须设置布局参数来显示东西。在添加到线性布局之前,请尝试将以下内容添加到图像视图中。

LinearLayout.LayoutParams layoutParameters = new LinearLayout.LayoutParams(
    LinearLayout.WRAP_CONTENT, 
    LinearLayout.WRAP_CONTENT); 

iv.setLayoutParams(layoutParams);
sv.addView( iv ); 
相关问题