android-在RelativeLayout中以编程方式放置两个彼此相邻的imageView

时间:2012-07-05 11:32:03

标签: android relativelayout layoutparams

我已经按原样创建了两个imageViews,如下所示:

        public void createImageViews(Integer count){



    ImageView[] imageViewArray = new ImageView[count];

    for (int i = 0; i < count; i++ ) {
        imageViewArray[i] = new ImageView(getBaseContext());
        imageViewArray[i].setId(i); // unique property for every imageView


        if(i==0){
            RelativeLayout.LayoutParams params =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            imageViewArray[i].setLayoutParams(params);
            imageViewArray[i].setBackgroundResource(imagesForIv[i]);
            _UIRLParent.addView(imageViewArray[i]);
            Log.v("first", "first"+i);
        }
        else if(i < 3){

            RelativeLayout.LayoutParams params =  new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
            imageViewArray[i].setBackgroundResource(imagesForIv[i]);
            _UIRLParent.addView(imageViewArray[i],params);
            Log.v("second", "second"+i);
        }

    }

我只需要将第二个imageView toRightOf放在第一个imageView上。有人能帮我吗。这吃掉了我很多时间。

1 个答案:

答案 0 :(得分:0)

尝试https://stackoverflow.com/a/5191159/1436931

您使用了错误的索引值。

在第

params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());

您将当前图像的当前图像右对齐。 :)

你需要跟踪最后一个imageView id的索引,即左图像视图

你需要这样的东西

params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i-1].getId());