开发者

Updating child views position into parent view at runtime

I've got a FrameLayout with TextView children and I'd like to change the position of the TextViews at runtime (in onSensorChanged(), so several time per seconds). There is no 开发者_Go百科setPosition(...), so how can I do that?

Thanks


All you need is View.layout(int,int,int,int) method.
Reference says:

public void layout (int l, int t, int r, int b)

Since: API Level 1
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring).  
In this phase, each parent calls layout on all of its children to position them.  
This is typically done using the child measurements that were stored  
in the measure pass().


FrameLayout isn't good for doing this sort of thing, with RelativeLayout you can do the following:

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

layoutParams.leftMargin = x;
layoutParams.topMargin = y;

child.setLayoutParams(layoutParams);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜