Android - 如何动态添加图像视图?

时间:2021-09-20 07:00:32

I would like to add an image view dynamically and then add a 2 bitmaps to that view, how can I do this?

我想动态添加一个图像视图,然后向该视图添加一个2位图,我该怎么做?

1 个解决方案

#1


9  

I don't think you can add two bitmaps to a single image view: it sounds like you'll need to create two image views to wrap each bitmap and position them how you want the two bitmaps to be relative to each other. Maybe surround the two ImageViews with a RelativeLayout.

我不认为你可以在单个图像视图中添加两个位图:听起来你需要创建两个图像视图来包装每个位图,并将它们放在两个位图相对于彼此的位置。也许用RelativeLayout围绕两个ImageViews。

Untested Pseudocode:

未经测试的伪代码:

RelativeLayout layout = new RelativeLayout(this);

// TODO: Set attributes for layout
// i.e. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
// layout.setLayoutParams(params);

ImageView imageView1 = new ImageView(this);
imageView1.setBackgroundResource(R.drawable.bitmap1); 

ImageView imageView2 = new ImageView(this);
imageView2.setBackgroundResource(R.drawable.bitmap2); 

// TODO: Set LayoutParams for each imageView
// i.e. RelativeLayout.LayoutParams imageParams1 = new RelativeLayout.LayoutParams(imageWidth, imageHeight); 
// imageParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
// imageParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

layout.addView(imageView1, imageParams1);
layout.addView(imageView2, imageParams2);

#1


9  

I don't think you can add two bitmaps to a single image view: it sounds like you'll need to create two image views to wrap each bitmap and position them how you want the two bitmaps to be relative to each other. Maybe surround the two ImageViews with a RelativeLayout.

我不认为你可以在单个图像视图中添加两个位图:听起来你需要创建两个图像视图来包装每个位图,并将它们放在两个位图相对于彼此的位置。也许用RelativeLayout围绕两个ImageViews。

Untested Pseudocode:

未经测试的伪代码:

RelativeLayout layout = new RelativeLayout(this);

// TODO: Set attributes for layout
// i.e. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
// layout.setLayoutParams(params);

ImageView imageView1 = new ImageView(this);
imageView1.setBackgroundResource(R.drawable.bitmap1); 

ImageView imageView2 = new ImageView(this);
imageView2.setBackgroundResource(R.drawable.bitmap2); 

// TODO: Set LayoutParams for each imageView
// i.e. RelativeLayout.LayoutParams imageParams1 = new RelativeLayout.LayoutParams(imageWidth, imageHeight); 
// imageParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
// imageParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

layout.addView(imageView1, imageParams1);
layout.addView(imageView2, imageParams2);