0

I have the following in xml. It is used in a relative layout:

  <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" />

In code, if I remove the 2nd textview (tv2), I want to reposition tv3 below tv1. I wasn't able to find a way to do this with xml, so I want to do it programmatically. How can I do this? Thanks a lot!

Johann
  • 27,536
  • 39
  • 165
  • 279

2 Answers2

0

I am assuming that it would be fairly similar to the question here -> How to set layout_weight attribute dynamically from code?

Community
  • 1
  • 1
Proxy32
  • 711
  • 5
  • 18
  • 1
    Actually, that's pretty close. That posting deals with weight which is irrelevant to RelativeLayout, which is probably why I didn't find that post. Still, the general solution is correct. Thanks a lot. – Johann May 04 '12 at 14:18
0

Lint reports performance issues with nested weight. Not really noticeable but here's another solution.


RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

p.addRule(RelativeLayout.BELOW, R.id.tv1);
tv3.setLayoutParams(p);

Fredrik
  • 1,282
  • 12
  • 15