android 解决 addView()操作之后,之前的控件位置重置的问题
这两天在给同事看一个问题。她在一个ViewA中加入一个子控件view1之后,然后通过手指滑动,来移动这个子控件,也就是改变了这个子控件的位置了。然后再加入另外一个子控件view2,这时候view1的位置被重置了,也就是跑到了viewA的左上角了。
同事给我讲了一下问题的基本情况之后,我的第一想法是子控件view1的位置属性并没有被记录下来。仔细的看了一下她移动子控件的代码,发现她是使用setLeft(),setTop()这两个方法来移动子控件的。随后去官网查了一下setTop这个方法的介绍:
Since: API Level 11
Sets the top position of this view relative to its parent. This method is meant to be called by the layout system and should not generally be called otherwise, because the property may be changed at any time by the layout.