我想將一個視圖從一個RelativeLayout容器移動到另一個RelativeLayout,在下面的代碼中名爲transparentOverlay。 (另一個是覆蓋圖,我打算用它來製作我的動畫)。Android重新定位視圖,錯誤的y值
這是我的佈局結構:
<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值是前3 70,現在是24(與rlLoc[1]
的值相同)。
另一件事是,與原始視圖相比,視圖的寬度和高度都變小了。
這是什麼原因?我需要做些什麼才能做到這一點?
你有沒有找到答案?我有同樣的問題 –