这不是实现它的最佳方式,但它是一个解决方案……
创建一个类扩展您的父视图组.比较RelativeLayout或其他一些ViewGroups.
在覆盖onFinishInflate()中找到自己的视图.
覆盖onLayout()方法并在调用super onLayout()后手动布局自己的视图.
最后每次要刷新自己视图的位置时,只需调用requestLayout()即可.
这是以下示例…
private AwesomeView mMyView;
private int mYourDesiredX;
private int mYourDesiredY;
@Override
public void onFinishInflate() {
super.onFinishInflate();
if (mMyView == null) {
mMyView = (AwesomeView)findViewById();
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
mMyView .layout(mYourDesiredX,
mYourDesiredY,
mYourDesiredX + mMyView.getWidth(),
mYourDesiredY + mMyView.getHeight());
}
但这不是一个有效的解决方案.
如果您正在制作游戏,请尝试使用SurfaceView甚至是GLSurfaceView,并使用Canvas自己绘制对象.