tags:

views:

853

answers:

1

I have a custom view, derived from Button, which I want to position at runtime, relative to another view. Because I don't know that other view's position yet when my view is being inflated (because layouting hasn't started), I leverage the onSizeChanged handler to set my view's position relative to the other view.

In onSizeChanged:

LayoutParams lp = new LayoutParams(this.getMeasuredWidth(), this.getMeasuredHeight());
lp.leftMargin = x;
lp.topMargin = y;
this.setLayoutParams(lp);

forceLayout();

That, however, has no effect. How come?

+1  A: 

Some things to consider:

  1. Are you sure this code is being executed?
  2. Have you examined the UI in hierarchyviewer to see if the margins are being set but they are simply not having the visual effect you expect?
  3. Are you sure this isn't better handled just via a RelativeLayout?
  4. Have you tried modifying the existing LayoutParams rather than replacing it?
CommonsWare
Hi Mark. 1,2: yes; 3: Can't do, I merge with the parent FrameLayout; 4: No, I'll give that a try.
Matthias