Android - 如何以编程方式为RelativeLayout指定权重?

时间:2011-05-20 14:26:20

标签: android relativelayout

我想以编程方式执行此类操作 -

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

它不允许我以编程方式执行setWeight。但是,我确实看到RelativeLayout在XML中有一个android:layout_weight参数。我错过了什么吗?

1 个答案:

答案 0 :(得分:27)

使用addView将RelativeLayout添加到LinearLayout时,您必须提供一个LinearLayout.LayoutParams,如下所示:

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

参考here