4

What is the difference between using setLayoutParams and supplying the parameters to addView?

I understand that addView only works when adding the view for the first time. I found two ways of apparently doing the same thing:

tv.setLayoutParams(params);
layout.addView(tv)

vs

layout.addView(tv, params)

Are they equivalent?
If not what are the differences?

user
  • 86,916
  • 18
  • 197
  • 190
hultqvist
  • 17,451
  • 15
  • 64
  • 101

1 Answers1

5

Are they equivalent?

Yes, the first method does an extra check to see if the View which is being added has LayoutParams set on it(and generate some LayoutParams if they don't exist). You could choose either one(I would pick the second method call).

user
  • 86,916
  • 18
  • 197
  • 190