Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why setWidth does not work?

For example, for a TextView in Android you have to set the LayoutParams instead of the setWidth method.

TextView tv = new TextView(getContext());
LayoutParams params = new LayoutParams(100, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);

But why? In the Android docs it says it would deliver the same result.

like image 627
Roy Avatar asked Sep 19 '14 20:09

Roy


1 Answers

You have to use layoutParams because setting a different width or height can change all the positioning of the layout. All the view hierarchy has to accommodate to the new dimensions, the only way is changing the layout params. The parent then gets notificated and starts all the process of measuring the views of the children.

like image 93
garlic Avatar answered Nov 12 '22 02:11

garlic