2011-05-20 61 views
7

我想这样做编程 -Android - 如何为RelativeLayout以编程方式指定权重?

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

它不会让我做了setWeight编程。不过,我确实看到RelativeLayout在XML中有一个android:layout_weight参数。我错过了什么吗?

回答

26

当你使用addView添加的RelativeLayout到的LinearLayout,你必须提供一个LinearLayout.LayoutParams,是这样的:

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

参考here

+2

工作就像一个魅力!感谢一堆:) – Suchi 2011-05-20 21:26:52

相关问题