2012-04-25 200 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);