2011-05-20 59 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

相關問題