在代码中,可以使用以下方法来移动控件的位置。
这两个方法都是基于控件当前位置进行位移的,需要注意的是,坐标系的原点在左上角,Y轴正方向向下,X轴正方向向右。
imageView.setTranslationX(10);
imageView.setTranslationY(-10);
/**
* Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
* This effectively positions the object post-layout, in addition to wherever the object's
* layout placed it.
*
* @param translationX The horizontal position of this view relative to its left position,
* in pixels.
*
* @attr ref android.R.styleable#View_translationX
*/
public void setTranslationX(float translationX) {
if (translationX != getTranslationX()) {
invalidateViewProperty(true, false);
mRenderNode.setTranslationX(translationX);
invalidateViewProperty(false, true);
invalidateParentIfNeededAndWasQuickRejected();
notifySubtreeAccessibilityStateChangedIfNeeded();
}
}