2012-02-15 55 views
0

我使用這下面的代碼添加imageViews之一,但它似乎沒有正常工作 我想用一個按鈕來添加圖像,然後顯示在特定relativeLayout不能由一個佈局

縮略圖
public void showViewOfReceiptFromSelecting(String uriString) 
    { 
     byte[] imageData = null; 
     try 
     { 
     InputStream fis = this.getContentResolver().openInputStream(Uri.parse((uriString))); 
     Bitmap imageBitmap = BitmapFactory.decodeStream(fis); 
     imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 40, 40, false); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     imageData = baos.toByteArray(); 
     ImageView image = new ImageView(this); 
     image.setImageBitmap(imageBitmap); 
     image.setId(counterOfReceipts); 
     RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     rlp.addRule(RelativeLayout.RIGHT_OF, counterOfReceipts - 1); 
     myRelalativelayout.addView(image, rlp); // a relative Layout i already defined earlier in the code 
     counterOfReceipts = counterOfReceipts + 1 ; 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } 
    } 

現在的問題是,每當我嘗試添加一個縮略圖時,它會替換舊的。請告訴我該怎麼辦...

問候

+0

你想添加多少... – 2012-02-15 10:32:31

+0

最多10個。 – 2012-02-15 10:33:43

+0

可以請你把整個代碼在這裏,所以我可以得到更好的解決方案,從 – 2012-02-15 10:38:46

回答

1

當然它被替換,因爲你沒有添加新的視圖佈局,只需替換它的形象。 嘗試用LinearLayout替換RelativeLayout,然後每當您想要添加新的縮略圖時,創建一個新的ImageView,將ImageView的背景設置爲您的位圖,然後將其添加到LinearLayout。

不要忘記定義您的LinearLayout方向。

+0

所以代碼是好的?只需將相對佈局類型更改爲線性? – 2012-02-15 11:05:13

+0

可能。 RelativeLayout並不在乎你是否在其中添加了一個新的視圖,這個視圖會覆蓋之前存在的另一個現有視圖。也許你的圖片只是彼此重疊。 – josephus 2012-02-15 11:14:35

相關問題