我需要将图像与另一个图像重叠。 以下是我的代码
int cards[] = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4,R.drawable.c5,R.drawable.c6,
R.drawable.c7,R.drawable.c8,R.drawable.c9,R.drawable.c10,R.drawable.c11,R.drawable.c12,R.drawable.c13};
ImageView[] Images = new ImageView[cards.length];
for (int i = 0; i < cards.length; i++) {
Images[i] = new ImageView(this);
Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
Images[i].setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i != 0) {
params.addRule(RelativeLayout.RIGHT_OF,Images[i - 1].getId());
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, Images[i - 1].getId());
layout.addView(Images[i], params);
} else {
params.addRule(RelativeLayout.LEFT_OF,Images[0].getId());
params.rightMargin=10;
layout.addView(Images[i]);
}
}
有了这个,我无法显示第一张图像,剩余的图像显示但不与其他图像重叠。 如何解决这个问题?
答案 0 :(得分:0)
最后能够借助此link
解决问题ImageView[] Images = new ImageView[cards.length];
for (int i = 0; i < cards.length; i++) {
Images[i] = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i != 0) {
params.addRule(RelativeLayout.ALIGN_TOP, i-1);
params.addRule(RelativeLayout.ALIGN_LEFT,i-1);
params.leftMargin= 40;
Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
Images[i].setId(i);
layout.addView(Images[i], params);
} else {
Images[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), cards[i]));
Images[i].setId(i);
layout.addView(Images[i], params);
}
}
答案 1 :(得分:-1)
代码完全错误:)你知道RIGHT_OF和LEFT_OF是什么意思吗? 只需删除所有if else部分,你应该看到所有te图像一个在另一个之上。 顺便说一下,有更好的方法可以像在LayerDrawable类上一样在Android上堆叠图像。