changing the position of ImageView dynamically in android

ⅰ亾dé卋堺 提交于 2019-12-11 00:13:34

问题


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?


回答1:


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);



回答2:


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


来源:https://stackoverflow.com/questions/6607190/changing-the-position-of-imageview-dynamically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!