2012-04-25 155 views
1

我正试图动态地将图像添加到滚动视图。要添加图像,滚动型我使用这个代码:如何动态添加图像到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 
|------------- 

为此,我需要改变滚动型莫名其妙内容的大小?

这是如何在Android中完成的?

谢谢。

+2

使用一个ListView。那是他们的。 – njzk2 2012-04-25 13:44:34

+0

也可以为ScrollView使用修正大小。 match_parent,最有可能的,而不是wrap_content,因为它可能只是长出屏幕 – njzk2 2012-04-25 13:45:33

+0

我不想使用listView我想使用ScrollView与图像背景的按钮。 感谢提示 – Streetboy 2012-04-25 13:48:54

回答

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

这基本上是他在做什么。 – njzk2 2012-04-25 13:46:07

+0

设置的LinearLayout垂直 – MAC 2012-04-25 13:48:38

+0

的这的确是他的XML问题的方向 – njzk2 2012-04-25 13:49:22

0

你能告诉我们现在发生了什么吗?它运行并没有显示出来?

我有,你必须设置布局PARAMS的东西展现出来预感。在添加到线性布局之前,尝试将以下内容添加到图像视图中。

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

iv.setLayoutParams(layoutParams); 
sv.addView(iv);