How can I dynamically set the position of view in

2019-01-01 15:49发布

How can I change the position of view through code? Like changing its X, Y position. Is it possible?

11条回答
千与千寻千般痛.
2楼-- · 2019-01-01 16:03

Yes, you can dynamically set the position of view in Android. Likewise you have an ImageView in LinearLayout of your xml file.So you can set its position through LayoutParams.But make sure to take LayoutParams according to the layout taken in your xml file.There are different LayoutParams according to the layout taken.

Here is the code to set:

    LayoutParams layoutParams=new LayoutParams(int width, int height);
    layoutParams.setMargins(int left, int top, int right, int bottom);
    imageView.setLayoutParams(layoutParams);

I hope it will be helpful to you to set position.

查看更多
看淡一切
3楼-- · 2019-01-01 16:03

There is a library called NineOldAndroids, which allows you to use the Honeycomb animation library all the way down to version one.

This means you can define left, right, translationX/Y with a slightly different interface.

Here is how it works:

ViewHelper.setTranslationX(view, 50f);

You just use the static methods from the ViewHelper class, pass the view and which ever value you want to set it to.

查看更多
与君花间醉酒
4楼-- · 2019-01-01 16:06

For support to all API levels you can use it like this:

ViewPropertyAnimator.animate(view).translationYBy(-yourY).translationXBy(-yourX).setDuration(0);
查看更多
初与友歌
5楼-- · 2019-01-01 16:11

For anything below Honeycomb (API Level 11) you'll have to use setLayoutParams(...).

If you can limit your support to Honeycomb and up you can use the setX(...), setY(...), setLeft(...), setTop(...), etc.

查看更多
弹指情弦暗扣
6楼-- · 2019-01-01 16:12

I found that @Stefan Haustein comes very close to my experience, but not sure 100%. My suggestion is:

  • setLeft() / setRight() / setBottom() / setTop() won't work sometimes.
  • If you want to set a position temporarily (e.g for doing animation, not affected a hierachy) when the view was added and shown, just use setX()/ setY() instead. (You might want search more in difference setLeft() and setX())
  • And note that X, Y seem to be absolute, and it was supported by AbsoluteLayout which now is deprecated. Thus, you feel X, Y is likely not supported any more. And yes, it is, but only partly. It means if your view is added, setX(), setY() will work perfectly; otherwise, when you try to add a view into view group layout (e.g FrameLayout, LinearLayout, RelativeLayout), you must set its LayoutParams with marginLeft, marginTop instead (setX(), setY() in this case won't work sometimes).
  • Set position of the view by marginLeft and marginTop is an unsynchronized process. So it needs a bit time to update hierarchy. If you use the view straight away after set margin for it, you might get a wrong value.
查看更多
浮光初槿花落
7楼-- · 2019-01-01 16:15

You can try to use the following methods, if you're using HoneyComb Sdk(API Level 11).

view.setX(float x);

Parameter x is the visual x position of this view.

view.setY(float y);

Parameter y is the visual y position of this view.

I hope it will be helpful to you. :)

查看更多
登录 后发表回答