Changing LayoutParams programmatically has no effect in Android. What for?

I have a custom view derived from Button , which I want to position at runtime, relative to another view. Since I do not yet know that the other position of the view when my view is inflated (since the layout has not started yet), I use the onSizeChanged handler to set the position of my view relative to another view.

In onSizeChanged :

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

This, however, has no effect. Why?

+4
source share
1 answer

Some things to consider:

  • Are you sure this code is executing?
  • You examined the user interface in hierarchyviewer to find out if the fields are set, but they just don't have the visual effect that you expect?
  • Are you sure this is not better handled through RelativeLayout ?
  • Have you tried modifying an existing LayoutParams rather than replacing it?
+4
source

All Articles