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

8

我想通过编程实现类似以下的功能 -

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

我无法通过编程方式设置权重(setWeight)。但是,我看到RelativeLayout在XML中有一个android:layout_weight参数。我是否遗漏了什么?

1个回答

27

当你使用addView将RelativeLayout添加到LinearLayout中时,你需要提供一个LinearLayout.LayoutParams,例如:

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

请在这里查看参考资料。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接