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?
Some things to consider:
hierarchyviewer
to see if the margins are being set but they are simply not having the visual effect you expect?RelativeLayout
?LayoutParams
rather than replacing it?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With