Android的重新定位来看,错误的y值 [英] Android repositioning view, wrong y-value

查看:236
本文介绍了Android的重新定位来看,错误的y值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动从一个RelativeLayout的容器到另一个RelativeLayout的视图,在下面的code命名transparentOverlay。 (另一种是覆盖,我打算使用,使我的动画的)。

I'm trying to move a view from one RelativeLayout container to another RelativeLayout, named transparentOverlay in the code below. (The other one is an overlay, which I intend to use to make my animations on).

这是我的布局结构:

<RelativeLayout 
   android:id="@+id/rootLayer"  
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="@dimen/activity_padding">
   <!-- The value of activity_padding is 16dp -->

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    ....
   </RelativeLayout>
    <!-- Transparent layer -->   
    <RelativeLayout
        android:id="@+id/transparentOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent" />

</RelativeLayout>

下面是code我使用的是:

Here is the code I'm using:

int[] prevLocForView = new int[2];
view.getLocationOnScreen(prevLocForView);

RelativeLayout rl = (RelativeLayout) rootLayer.findViewById(R.id.transparentOverlay);
int[] rlLoc = new int[2];
rl.getLocationOnScreen(rlLoc);

rl.addView(view);
RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = currentLocForLetterView[0] - rlLoc[0];
layoutParams.topMargin = currentLocForLetterView[1] - rlLoc[1];

有得到显示在容器的正确位置,但是,它返回从以下code进行错误y值:

It get displayed at the correct position in the container, however, it returns a wrong y-value from the following code:

int[] temp = new int[2];
view.getLocationInWindow(temp);

x值是正确的,但y值是previous 370,现在是24(这是相同的值 rlLoc [1]

另一件事是,认为获取宽度和高度较大,相比原来的看法。

Another thing is that the view gets smaller in width and larger in height, compared to the original view.

什么是这个原因?什么我需要做的就是这个吧?

What's the reason for this? What do I need to do to get this right?

推荐答案

请注意,这是不是比<一个同样的问题href=\"http://stackoverflow.com/questions/31069733/how-to-force-view-to-recalculate-its-position-after-changing-parent\">this 之一,但解决可能是相同的。

Please note that this is not the same issue than this one, but the solution might the same.

我做了一个简单的测试。与此步步高板。

I did a simple test. with this backgammon board.

然后:


  1. 将在左边的第一个芯片(红色的底部)向右点

  2. 将在左边第二个芯片(蓝色在底部)向右点

  3. 将第三芯片的向右点的第一个位置

  1. Move the first chip in the left (the red one in the bottom) to the right point
  2. Move the second chip in the left (the blue one in the bottom) to the right point
  3. Move the third chip to the first position of the right point

所以,这是不言而喻

4          0     --->     4          3
3          0     --->     0          1
2          0     --->     0          2
1          0     --->     0          0

如你所见,结果并不确定,所有的芯片保持previous父内原来的位置。

As you see, results are not OK, all the chips keep the original position within their previous parent.

在另一方面,在视图被添加并正在正常显示芯片(可以连续看到,因为第三招两个红色芯片)

On the other hand, the View is being added and the chips are being displayed properly (you can see two red chips in a row, because of the third move)

其实这是因为只是把芯片在正确的点不使他们强迫布局。你需要的添加到onLayoutChangeListener你移动这样你就可以到布局的变化做出反应已经完成之后,所以,你已经得到的最终位置后,查看

Actually this happens because just putting the chips in the right point doesn't make them force their layout. You need to add an onLayoutChangeListener to the view you're moving so you can react to the layout change after it has been performed, and so, after you've gotten the final position:

public class YourView extends View {
private int[] currentLocation = new int[2];
/*.....
  * blah,blah,blah
  * .....*/

public void init() {
    this.addOnLayoutChangeListener(new OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            int[] newLocation = new int[2];
            v.getLocationOnScreen(newLocation);
            Log.d("New location:", "("+newLocation[0]+","+newLocation[1]+")");
            /** Do whatever is needed with old and new locations **/
            currentLocation = newLocation;
        }
    });
}

这篇关于Android的重新定位来看,错误的y值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆