So let’s say that I want to create multiple TextViews programatically inside a relative layout. It looks like with each new TextView I also have to create a new LayoutParams like so:
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Then I add whatever rules I want using:
p.addrule(...,...);
It seems that I cannot use this single LayoutParams to set the rules for multiple TextViews. Is this a true statement?
Thanks,
Using the same LayoutParams for multiple views is fine, modulo the caveat that changing the LayoutParams before the views have been through layout will apply the changes to all the views.
If you’re just looking to save code then you may look into LayoutParams’ copy constructor. This allows you to create a new LayoutParams from the data in another LayoutParams without having the two refer to the same LayoutParams instance.