May I reuse LayoutPrams with ViewGroup.addView?
There is nothing about it in apidoc.
This means you need to make the more conservative choice, no matter what the current implementation is, as the implementation could change.
Hence, you need to assume that it is not safe to reuse an instance of LayoutParams
with different Views
.
For what it's worth, as far as I can tell, that is true anyway - ViewGroup
doesn't make a copy.
This is an old question, but there seems to be an updated answer:
The LayoutParams has a constructor for copying another Source: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
ViewGroup.LayoutParams(ViewGroup.LayoutParams source)
This would suggest not to re-use it, but perhaps to create 1 layout params object with everything you need, then just call
new LayoutParams(someLayoutParamsToReUse)
In my case, I wanted to set the layout params of a button to be the same as another button. I initially tried:
button.setLayoutParams(button2.getLayoutParams());
This will not work, however this should:
button.setLayoutParams(new LayoutParms(button2.getLayoutParams))'