我想动态添加一个 ImageView ,然后向该 View 添加 2 个位图,我该怎么做?
最佳答案
我认为您不能将两个位图添加到单个 ImageView :听起来您需要创建两个 ImageView 来包装每个位图并按照您希望两个位图彼此相对的方式定位它们.也许用 RelativeLayout 包围两个 ImageView。
未经测试的伪代码:
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);
关于Android - 如何动态添加 ImageView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7362992/