将视图从线性布局重新定位到相对布局

时间:2018-07-11 23:05:49

标签: android android-layout view

我有两个Layouts,一个带有垂直方向和relativeLayout的linearLayout, 并且我使用getLocationOnScreen

将视图从linearLayout动态移动到relativeLayout
                    int[] cellLocation = new int[2];
                    targetCellImage.getLocationOnScreen( cellLocation );
                    int posX = cellLocation[0];
                    int posY = cellLocation[1];
                    //change the tag for the cell (black or white)
                    targetCellImage.setTag( currentPlayer );

            //remove any previous rules related to the stone previous layout
                    layoutParams2.removeRule( 
                       RelativeLayout.ALIGN_PARENT_RIGHT );
                    layoutParams2.removeRule( RelativeLayout.ALIGN_PARENT_END );
                    layoutParams2.setMargins( 0, 0, 0, 0 );
                    //reset padding and make the stone move
                    stone.setPadding( 0, 0, 0, 0 );
                    if (stone.getParent() != null) ((ViewGroup) 
                       stone.getParent()).removeView( stone );
                    mainLayout.addView( stone, layoutParams2 );

                    //set the stone tag to the Id of the current cell
                    // move the stone to that cell
                    stone.setTag( targetCellIDName );
                    stone.setX( posX);
                    stone.setY( posY);

除了我意外输入的一个问题外,此代码可以正常工作,当我单击LinearLayout中的第一个视图时,它应正确地移到了应该的位置,但是如果我单击了最后一个视图,则y轴将以某种方式移动并且新的位置不正确。

I click this view and the new position is correct

but when I click the las view it is not moving to the same prev pos

为什么会这样...我希望我的问题很清楚

0 个答案:

没有答案