Does ViewGroup.addView
clones LayoutParams
data into inside or links to it? May I reuse the same instance of LayoutParams
with multiple calls to addView()
with different views?
There is nothing about it in apidoc.
WOW
The answer is NO (checked experimentally):
public class SymbolPadActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout.LayoutParams labelParams;
/*
* This block to reuse is not working
labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
*/
RelativeLayout mover = new RelativeLayout(this);
TextView textView;
for(int leftMargin = 0; leftMargin<3000; leftMargin += 100) {
for(int topMargin=0; topMargin<800; topMargin += 40) {
// I can't omit these 3 lines
labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
labelParams.leftMargin = leftMargin;
labelParams.topMargin = topMargin;
textView = new TextView(this);
textView.setText("(" + leftMargin + "," + topMargin + ")");
mover.addView(textView, labelParams);
}
}
RelativeLayout.LayoutParams moverParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
moverParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
moverParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
moverParams.leftMargin = 0;
moverParams.topMargin = 0;
RelativeLayout stator = new RelativeLayout(this);
stator.addView(mover, 0, moverParams);
setContentView(stator);
}
}
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))'
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.
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