在android中动态改变ImageView的位置

发布于 2024-11-18 21:01:38 字数 370 浏览 3 评论 0原文

我需要动态

更改 ImageView 的位置,并使用以下代码,

int x=100,y=100;
RelativeLayout.LayoutParams mparam = new RelativeLayout.LayoutParams((int)(LayoutParams.FILL_PARENT),(int)(LayoutParams.FILL_PARENT));
    mparam.topMargin=x;
    mparam.leftMargin=y;
    ball.setLayoutParams(mparam);
    x+=100;
    y+=100;

但我没有得到任何更改。

是否可以?以及如何?

all

I need to change the position of a ImageView dynamically and iam using the following code

int x=100,y=100;
RelativeLayout.LayoutParams mparam = new RelativeLayout.LayoutParams((int)(LayoutParams.FILL_PARENT),(int)(LayoutParams.FILL_PARENT));
    mparam.topMargin=x;
    mparam.leftMargin=y;
    ball.setLayoutParams(mparam);
    x+=100;
    y+=100;

but i didnt get any change.

Is it possible? and How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-11-25 21:01:38

您更改了保证金,但没有更改其位置,这似乎不是一个好的解决方案。
尝试使用方法 View.layout(int, int, int, int)
文档 说:

public void layout (int l, int t, int r, int b)

Since: API Level 1
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring). 
In this phase, each parent calls layout on all of its children to position them. 
This is typically done using the child measurements that were stored in 
the measure pass(). Derived classes should not override this method. Derived 
classes with children should override onLayout. In that method, they should call 
layout on each of their children.

Parameters

l   Left position, relative to parent
t   Top position, relative to parent
r   Right position, relative to parent
b   Bottom position, relative to parent

You change margin, but not its position and, it seems, it's not a good solution.
Try to use method View.layout(int, int, int, int)
Documentation says:

public void layout (int l, int t, int r, int b)

Since: API Level 1
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring). 
In this phase, each parent calls layout on all of its children to position them. 
This is typically done using the child measurements that were stored in 
the measure pass(). Derived classes should not override this method. Derived 
classes with children should override onLayout. In that method, they should call 
layout on each of their children.

Parameters

l   Left position, relative to parent
t   Top position, relative to parent
r   Right position, relative to parent
b   Bottom position, relative to parent
乜一 2024-11-25 21:01:38

您可以在视图上调用 setPadding。因此,在您的代码中(顺便说一下,您应该放入代码块!),只需添加对此方法的调用:

int x=100,y=100; 
LayoutParams mparam = new LayoutParams((int)(LayoutParams.FILL_PARENT),(int) (LayoutParams.FILL_PARENT));
ball.setLayoutParams(mparam); 
ball.setPadding(x,y,0,0);

x+=100; y+=100;
ball.setPadding(x,y,0,0);

You can call setPadding on your View. So, in your code (which by the way you should put in a code block!), just add a call to this method:

int x=100,y=100; 
LayoutParams mparam = new LayoutParams((int)(LayoutParams.FILL_PARENT),(int) (LayoutParams.FILL_PARENT));
ball.setLayoutParams(mparam); 
ball.setPadding(x,y,0,0);

x+=100; y+=100;
ball.setPadding(x,y,0,0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文