【问题标题】:Android - How do I specify weight programmatically for a RelativeLayout?Android - 如何以编程方式为 RelativeLayout 指定权重?
【发布时间】:2011-05-20 14:26:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

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

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

    参考here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-28
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多