更改按钮的位置按钮、位置

2023-09-12 08:13:30 作者:失心疯

我在一个AbsoluteLayout一个按钮在XML文件中。从那里,我能够设置按钮(X,Y)的位置。

I have one button in an AbsoluteLayout in an XML file. From there I am able to set the (x,y) position of the button.

我怎样才能获取和设置(X,Y)坐标按钮编程?

How can I get and set the (x,y) coordinates of the button programmatically?

谢谢大家。

推荐答案

您必须通过调用findViewById().当你得到了参照按钮,你可以设置x和y的值按钮的 setX的()和按钮。塞蒂()

You have to get a reference to you button, for example by calling findViewById(). When you got the reference to the button you can set the x and y value with button.setX() and button.setY().

....
Button myButton = (Button) findViewById(R.id.<id of the button in the layout xml file>);
myButton.setX(<x value>);
myButton.setY(<y value>);
....