Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between setting layoutparams direct or in addView

Tags:

android

I was wondering if there is a difference between

LinearLayout.LayoutParams separatorParams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

TextView separator = new TextView(context);
separator.setLayoutParams(seperatorParams);

this.addView(separator);

Or

LinearLayout.LayoutParams separatorParams = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

TextView separator = new TextView(context);

this.addView(separator,separatorParams);
like image 894
Robby Smet Avatar asked Feb 18 '26 14:02

Robby Smet


2 Answers

According to the source code, it basically makes no difference since addView(View view) calls addView(view, view.getLayoutParams())

like image 186
njzk2 Avatar answered Feb 20 '26 03:02

njzk2


Absolutely none.

this.addView(separator); Will grab the params from the child or generate it.

Where as: this.addView(separator,separatorParams); Will use the params then add it to the layout anyway.

Have a look at the source here

like image 45
Chris.Jenkins Avatar answered Feb 20 '26 03:02

Chris.Jenkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!