如何在textview中以编程方式设置layout_toEndOf和layout_toRightOf

时间:2015-12-07 11:40:46

标签: android

我想以编程方式设置这些属性: -

android:layout_toEndOf="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"

1 个答案:

答案 0 :(得分:14)

View的父级当然必须是RelativeLayout。然后就可以了

 final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
 params.addRule(RelativeLayout.RIGHT_OF, R.id.imageView1);
 params.addRule(RelativeLayout.END_OF, R.id.imageView1);

并致电

view.setLayoutParams(params);