Android - How to specify weight programmatically for RelativeLayout?

I want to do something like this programmatically -

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

This does not allow me to do setWeight programmatically. However, I see that RelativeLayout has an android: layout_weight parameter in XML. Did I miss something?

+8
android relativelayout
source share
1 answer

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

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

Link here

+27
source share

All Articles