8

I want to do something like this programmatically -

<LinearLayout>
  <RelativeLayout1 weight = 1>
  <RelativeLayout2 weight = 3>
  <RelativeLayout3 weight = 1>
<LinearLayout>

It does not let me do a setWeight programmatically. However, I do see that RelativeLayout has an android:layout_weight parameter in XML. Am I missing something?

Suchi
  • 9,989
  • 23
  • 68
  • 112

1 Answers1

27

When you add the RelativeLayout to the LinearLayout using addView you have to provide a LinearLayout.LayoutParams, something like this :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
linearLayout.addView(relativeLayout, params);

Reference here

biddulph.r
  • 5,226
  • 3
  • 32
  • 47
martiall
  • 981
  • 6
  • 7