在运行时将子视图位置更新为父视图

时间:2011-07-18 15:44:09

标签: android view position

我有一个带有TextView子元素的FrameLayout,我想在运行时更改TextViews的位置(在onSensorChanged()中,所以每秒几次)。没有setPosition(...),所以我该怎么做?

由于

2 个答案:

答案 0 :(得分:0)

您需要的只是View.layout(int,int,int,int)方法 Reference说:

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().

答案 1 :(得分:0)

FrameLayout做这类事情并不好,RelativeLayout您可以执行以下操作:

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

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

child.setLayoutParams(layoutParams);