改变按钮的位置

WIS*_*ISH 7 android coordinates absolutelayout

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

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

谢谢大家.

Flo*_*Flo 10

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

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


kco*_*ock 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);
Run Code Online (Sandbox Code Playgroud)

或者:

Button button = (Button)findViewById(R.id.my_button);
button.setLayoutParams(new AbsoluteLayout.LayoutParams(
    AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams,
    myNewX, myNewY));
Run Code Online (Sandbox Code Playgroud)


小智 0

嗯,你可以试试这个。。。 有没有一种简单的方法可以在 Android 视图的顶部和底部添加边框? 只要去这个网站你就会得到解决方案。如果没有那就回复我。如果它对你有帮助,请给我投票。谢谢。