【问题标题】:Android: Reposition control below another control programmaticallyAndroid:以编程方式将控件重新定位到另一个控件下方
【发布时间】:2012-05-04 22:27:07
【问题描述】:

我在 xml 中有以下内容。它用于相对布局:

  <TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>        
  <TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv1"/>
  <TextView
    android:id="@+id/tv3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv2" />

在代码中,如果我删除第二个 textview (tv2),我想将 tv3 重新定位到 tv1 下方。我无法找到使用 xml 执行此操作的方法,因此我想以编程方式执行此操作。我怎样才能做到这一点?非常感谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    我假设它与这里的问题非常相似 -> How to set layout_weight attribute dynamically from code?

    【讨论】:

    • 实际上,这非常接近。该帖子涉及与RelativeLayout无关的重量,这可能是我没有找到该帖子的原因。不过,一般的解决方案是正确的。非常感谢。
    【解决方案2】:

    Lint 报告嵌套权重的性能问题。不是很明显,但这是另一种解决方案。

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); p.addRule(RelativeLayout.BELOW, R.id.tv1); tv3.setLayoutParams(p);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2011-04-17
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多