使用x和y坐标以编程方式在视图中设置项目位置

时间:2012-03-13 13:26:24

标签: android android-layout

我有一个ImageButton,我想将其位置编程设置为x和y。

我该怎么做?我读了一些abaut setTop()和setLeft(),但不知道如何使用它。

谢谢。

1 个答案:

答案 0 :(得分:1)

AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
         int width, int height, int x, int y);
params.x=30;
params.y=50;

imageButton.setLayoutParams(params);

使用此代码,您可以在视图中以绝对(x,y)坐标定位项目,但是您需要使用主根布局AbsoluteLayout,否则会抛出错误。

但AbsoluteLayout在最新版本中已经过折旧,所以要避免这种情况,尝试使用RelativeLayout

相关问题