Android重新定位 View ,错误的y值

标签 android android-layout android-view

我正在尝试将一个 View 从一个 RelativeLayout 容器移动到另一个 RelativeLayout,在下面的代码中名为 transparentOverlay。 (另一个是叠加层,我打算用它来制作动画)。

这是我的布局结构:

<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>

这是我使用的代码:

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

它显示在容器中的正确位置,但是,它从以下代码返回错误的 y 值:

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

x 值是正确的,但 y 值以前是 370,现在是 24(与 rlLoc[1] 的值相同)。

另一件事是,与原始 View 相比, View 的宽度变小,高度变大。

这是什么原因?我需要做什么才能做到这一点?

最佳答案

请注意,这与 this one 不是同一个问题, 但是 the solution可能一样。

我做了一个简单的测试。有了这个西洋双陆棋棋盘。

a backgammon board draft with some chips in a point

然后:

  1. 将左边的第一个筹码(底部的红色筹码)移到右边的点
  2. 将左边的第二个筹码(底部的蓝色筹码)移到右边的点
  3. 将第三个筹码移到右边点的第一个位置

就这样吧

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

backgammon after moving chips around

如您所见,结果不正常,所有芯片都保持在其先前父级中的原始位置。

另一方面,View正在添加,筹码正常显示(可以看到连续两个红色筹码,因为第三步)

实际上,发生这种情况是因为仅仅将筹码放在正确的位置并不能使它们强制布局。您需要将一个 onLayoutChangeListener 添加到您正在移动的 View ,以便您可以在布局更改执行后使用react,因此,在您获得最终位置之后:

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重新定位 View ,错误的y值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20228834/

相关文章:

android - 在 android 中隐藏/显示 View 的问题

android - setVisibility() 和 setAlpha() 之间的区别

java - Android自定义背景导致列表适配器出现数字格式异常

android - 应用程序为 Facebook 登录配置错误 : Android Facebook integration issue

android - 不一致的处理程序行为

android - android :gravity ="center_vertical" for RelativeLayout and LinearLayout 的行为差异

android - 在 res/drawable 文件夹中添加图像后,R.java 文件消失

android - 如何在 Windows 上终止 adb.exe?怎么老是重启

android - ScrollView 中的 ExpandableListView

java - 如何从 android TimeSquare 库中获取选定的日期?