我想使用一个按钮添加一个图像,然后在一个指定的 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();
}
}
现在的问题是,当我试图再添加一个缩略图时,它就会取代了之前的那个旧的。我改怎么处理这个问题?
rlp属性是否是需要设置为ALIGN_WITH_PARENT_LEFT或者ALIGN_WITH_PARENT_RIGHT
当然会取代了,因为你没有添加一个新的视图到布局中,只是替换了图像。使用一个 LinearLayout 代替RelativeLayout,然后当你想添加一个新的缩略图时,创建一个新的 ImageView,在 bitmap中设置ImageView 的背景,然后添加到 LinearLayout 中。
不要忘记定义 LinearLayout 方向。