2013-11-26 15 views
2

我想將一個視圖從一個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]的值相同)。

另一件事是,與原始視圖相比,視圖的寬度和高度都變小了。

這是什麼原因?我需要做些什麼才能做到這一點?

+0

你有沒有找到答案?我有同樣的問題 –

回答

0

該值始終與視圖的父項相關。 所以,你必須計算新的y值的位置,並設置。你不能只使用相同的。

+0

這就是我所做的:params.topMargin = prevLocForView [1] - flLoc [1]; 如果我檢查topMargin的值,它是正確的值,370. – moviaa

+0

來自FrameLayout doc:FrameLayout的大小是其最大子項(加上填充)的大小,可見與否(如果FrameLayout的父級允許)。所以,你應該計算和使用填充,而不是保證金。 –

+0

感謝您的回覆。視圖顯示在正確的位置。但是,當我檢查移動視圖的getLocationInWindow()時,返回的X值是正確的,但不是Y值。 如何在創建視圖時在寬度和高度上添加值8? – moviaa

1

請注意,這不是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

正如你看到的,結果都不行,所有的芯片保持着先前的母公司內原來的位置。

在另一方面,查看被加入,並正在正常顯示芯片(可以連續看到兩個紅籌股,因爲第三招)

其實出現這種情況,因爲只把芯片在正確的方面不會讓他們強迫他們的佈局。您需要添加onLayoutChangeListener你正在移動,所以你可以把它的處理之後的佈局變化作出反應的觀點,所以,你已經得到的最終位置後:

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; 
     } 
    }); 
} 
相關問題