0

我已经创建了两个图像视图,如下所示:

        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 放在第一个 imageView 的 RightOf 上。有人能帮我吗。这占用了我很多时间。

4

1 回答 1

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());

于 2012-07-05T11:34:45.530 回答