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)
你正在寻找的答案是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)
| 归档时间: |
|
| 查看次数: |
60311 次 |
| 最近记录: |