简体   繁体   English

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

[英]Set an item position in the view programmatically with the x and y coordinates

I have an ImageButton and I would like to set its position programmatically to x and y.我有一个 ImageButton,我想以编程方式将其位置设置为 x 和 y。

How can I do this?我怎样才能做到这一点? I read something abaut setTop() and setLeft() but don't know how to use it.我读了一些关于 setTop() 和 setLeft() 的东西,但不知道如何使用它。

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

imageButton.setLayoutParams(params);

using this code you can position you item in absolute (x,y) coordinate in your view, but you need to use you main root layout AbsoluteLayout otherwise it throws error.使用此代码,您可以在视图中以绝对 (x,y) 坐标定位您的项目,但您需要使用主根布局 AbsoluteLayout 否则会引发错误。

But AbsoluteLayout is depreciated in latest version so avoid that , try to use RelativeLayout但是 AbsoluteLayout 在最新版本中已贬值,因此请避免这种情况,请尝试使用 RelativeLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM