改变按钮的位置

时间:2011-04-13 09:15:16

标签: android coordinates absolutelayout

我在XML文件的AbsoluteLayout中有一个按钮。从那里我可以设置按钮的(x,y)位置。

如何以编程方式获取和设置按钮的(x,y)坐标?

谢谢大家。

4 个答案:

答案 0 :(得分:10)

您必须获得对您​​的引用按钮,例如通过调用findViewById()。当您获得对该按钮的引用时,您可以使用按钮setX()和按钮setY()设置x和y值。

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

答案 1 :(得分:5)

您正在寻找的答案位于LayoutParams。首先,我建议使用AbsoluteLayout - 它已被弃用 - 并使用其他东西,可能是FrameLayout,只使用左边距和上边距作为x和y偏移。

然而,要回答你的问题:

Button button = (Button)findViewById(R.id.my_button);
AbsoluteLayout.LayoutParams absParams = 
    (AbsoluteLayout.LayoutParams)button.getLayoutParams();
absParams.x = myNewX;
absParams.y = myNewY;
button.setLayoutParams(absParams);

或者:

Button button = (Button)findViewById(R.id.my_button);
button.setLayoutParams(new AbsoluteLayout.LayoutParams(
    AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams,
    myNewX, myNewY));

答案 2 :(得分:0)

嗯,你可以试试这个。 。 。 Is there an easy way to add a border to the top and bottom of an Android View? 只是去扔这个网站你会得到Sollution。如果没有,那么回复我。 如果它的帮助你然后给我一个投票。 感谢。

答案 3 :(得分:0)