2014-01-08 44 views
0

我有ImageView。我在設置新的位圖上更改ImageVew的邊距和大小。例如,用戶點擊按鈕時,我設置位圖的ImageView和改變PARAMS(這是我自己的看法代碼):如何在更改視圖大小時更改LayoutParams

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams(); 
p.leftMargin = (int) ((pw - lw)/2f); 
_topMargin = (int) ((ph - lh)/2f); 
lp.topMargin = _topMargin; 
lp.width = lw; 
lp.height = lh; 
requestLayout(); 

的所有工作。但我也想改變參數,如果改變我的ImageView的大小父視圖。如果我嘗試在這個event的變化參數上使用代碼 - 沒有任何待辦事項(ImageView不會改變參數)。

我該如何解決這個問題?

我現在看到一種方法:在更改父大小時,我不更改ImageView參數,只設置一些內部布爾標誌。完成父級更改後,我更改了ImageView參數。但我不知道,我需要觀看哪些父事件,之後我可以更改ImageView參數。

我的解決方案。

我用this方法。

回答

1
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams(); 
lp.leftMargin = (int) ((pw - lw)/2f); 
_topMargin = (int) ((ph - lh)/2f); 
lp.topMargin = _topMargin; 
lp.width = lw; 
lp.height = lh; 
setLayoutParams(lp);//<---set layout params 
requestLayout(); 

更改佈局參數後,您需要將其設置回來。