Pergunta

I need to overlap images one image by another. Below is my code

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]);
            }

       }

With this i am not able to display first image and remaining images are displayed but not overlapped with other images. How to fix this?

Foi útil?

Solução

Finally able to fix the issue with help of this 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);
            }

        }

Outras dicas

The code is totally wrong :) do you know what RIGHT_OF and LEFT_OF mean? just remove all the if else part and you should see all te images one on top of the other. Btw there are better ways to stack images on android like the LayerDrawable class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top