2013-04-23 42 views
-1

我目前在我的應用程序中有一個RelativeLayout,我動態地移動,調整大小和隱藏。移動一次之後,之後將佈局重新定位到奇怪的位置。LayoutParams在奇怪的位置放置視圖

這是我使用的佈局。

<RelativeLayout ...> 
    <RelativeLayout android:id="@+id/moveable_layout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:visibility="gone" /> 
</RelativeLayout> 

這裏是我使用它,重新定位

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, newHeight); 
m_movingFrame.setEnabled(true); 
m_movingFrame.setLayoutParams(layoutParams); 
m_movingFrame.setX(newX); 
m_movingFrame.setY(newY); 
m_movingFrame.requestLayout(); 
m_movingFrame.bringToFront(); 
m_movingFrame.setVisibility(View.VISIBLE); 

的代碼,這裏是用來隱藏佈局

m_movingFrame.setEnabled(false); 
m_movingFrame.setVisibility(View.GONE); 

的第一次,我重新定位它的代碼,它會移動到正確的位置,但每次後續的重新定位都會將其移動到錯誤的位置。誰能告訴我爲什麼它表現得如此不正常?

在此先感謝!

+0

你沒問這個問題嗎? – Luksprog 2013-04-23 15:24:57

+0

是的,但它沒有得到任何迴應,所以我用更具體的標題重新創建了它。 – loadedion 2013-04-23 15:45:14

+0

請不要這樣做,編輯並改進您的原始問題。 – Luksprog 2013-04-23 15:49:48

回答

0

首先獲取現有佈局的佈局,然後對其進行修改。

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) m_movingFrame.getLayoutParams(); 

... 

m_movingFrame.setLayoutParams(layoutParams); 
+0

謝謝,這似乎部分工作。但是,添加規則時,它似乎會更改視圖的父級。例如,設置'layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);'第一次將會使視圖居中,但如果我將它移動到某個位置然後重新居中,那麼它會在其他位置移動 – loadedion 2013-04-23 15:39:33