1

Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. So we LinearLayout.LayoutParams, RelativeLayout.LayoutParams, FrameLayout.LayoutParams etc.

Suppose I want to create a View, e.g. TextView programmatically. I will have to give it the required properties, like layout_width and layout_height, for which I will have to use a LayoutParams class. How will I know which LayoutParams class to use?

Solace
  • 8,612
  • 22
  • 95
  • 183

2 Answers2

4

It depends on what you're putting it into. If you're putting it into a linear layout, use LinearLayout.LayoutParams. For relative, use RelativeLayout.LayoutParams.

If you don't know the type- add it first via add, then use getLayoutParams to get it. Then change the layourparams. When you use the add() that doesn't take a params, the parent will make one for you.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
3

If you only need view width and height, use a ViewGroup.LayoutParams, as all the other ones inherit from this one. You can assign a ViewGroup.LayoutParams where a RelativeLayout.LayoutParams, LinearLayout.LayoutParams, ... is expected.

Francesc
  • 25,014
  • 10
  • 66
  • 84