Android:以编程方式将控件重新定位到另一个控件之下
我有以下xml格式。它用于相对布局中:Android:以编程方式将控件重新定位到另一个控件之下,android,Android,我有以下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:la
<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" />
在代码中,如果我删除第二个文本视图(tv2),我想将tv3重新定位到tv1下方。我找不到一种用xml实现这一点的方法,所以我想通过编程实现。我该怎么做?非常感谢 我假设它与这里的问题非常相似->Lint报告嵌套权重的性能问题。不是很明显,但这里有另一个解决方案
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.tv1);
tv3.setLayoutParams(p);
事实上,这很接近。那篇帖子是关于重量的,它与RelativeLayout无关,这可能就是我没有找到那篇帖子的原因。不过,总的解决方案是正确的。谢谢。