开发者

View moved but still on the top left corner

I'm adding a custom View to the parent layout, witch is a RelativeLayout.

I also need to move this View on the screen so I do:

myView.layout(xx, yy, width, height);

This works. The view is moved each time I call this.

The Problem is that when this view is moved,

a copy of it keeps hanging on the top left corner of the layout,

like this:

View moved but still on the top left corner

Why this happens and how can I prevent this?

Thank yo开发者_运维技巧u


-- Edit --

Some code below.

In the constructor of my custom View:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
inflater.inflate(R.layout.ipmaps_lib_balloon_overlay, this);

I then add this View to its parent:

final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parentLayout.addView(myView, params);  

When I need to move the View I just do:

myView.layout(startX, startY, width, height);
myView.invalidate  


According to the documentation, the layout method should be used in the onLayout phase of the view placement in a viewgroup. If you want to move the view temporarily to another location, use offsetLeftAndRight(int offset) and offsetTopAndBottom(int offset).

If that does not help, you will have to setLayoutParams for the view.

RelativeLayout.LayoutParams params = view.getLayoutParams();
params.setMargins(int left, int top, int right, int bottom);
view.setLayoutParams(params);


I found the reason for this strange behaviour.
It was my fault.

Whenever I changed the position of this message balloon,
I was as well calling view.draw(canvas),
and for this reason, the view was being drawn twice.

Thank you all very much.


I think you forgot to request a drawing pass. Call invalidate() on your myView.


You could use a TranslateAnimation to move your view. and if you have setFillAfterEnabled(true); then it will stay where ever you translate it to once it gets there.


Using exact locations with RelativeLayout looks like a dirty hack. I think you should use AbsoluteLayout instead. It's deprecated but it's used in some Android apps (e.g. Browser), so I don't think it will be removed. But if you don't want to use deprecated classes, you can just copy its sources into your project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜