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



我想以编程方式做这样的事情 -

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

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

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

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

参考这里

最新更新