On some interfaces, we may need to implement movable buttons and other functions. The simplest way is to call View. layout (), any layout space can be moved in this way. The value of the upper, lower, and left parameters is relative to the parent viewgroup.
public void layout(int l, int t, int r, int b)eg.button.layout(newLeft, newTop, newRight,newBottom);
However, this method has a defect that when there is a control update interface refresh in the same viewgroup, The moved control will be reset, that is, back to the starting position, which is often puzzling.
Another method is to call MarginLayoutParams. setMargins () and reset the control location parameter to achieve the control moving effect. This method is suitable for RelativeLayout, FrameLayout, and AbsoluteLayout. For LinearLayout, because the added control is always at the bottom or rightmost, the Movement effect cannot be reached, and TableLayout is not supported.
public MarginLayoutParams(MarginLayoutParams source)eg.FrameLayout.LayoutParams btnLp = (FrameLayout.LayoutParams)button.getLayoutParams();btnLp.setMargins(newLeft, newTop, newRight, newBottom); button.requestLayout();
Android does not recommend that the application directly call the layout () method. Therefore, I personally suggest using FrameLayout + setMargins () to achieve dynamic control movement. Another frequently used method is bringToFront (), it is useful when the control is covered and invisible.
This article is from "the power comes from the sincere love !" Blog, please be sure to keep this source http://stevex.blog.51cto.com/4300375/1266926