2014-01-08 49 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(); 

更改布局参数后,您需要将其设置回来。