以编程方式添加约束

时间:2017-09-25 08:39:25

标签: android android-layout android-constraintlayout

具有约束布局的布局定义中使用的以下XML指令的java代码的翻译是什么?

app:layout_constraintBottom_toBottomOf="@+id/Button1"
app:layout_constraintTop_toTopOf="@+id/Button1"
app:layout_constraintLeft_toLeftOf="@+id/Button2"
app:layout_constraintRight_toRightOf="Button2"

1 个答案:

答案 0 :(得分:9)

以下是以编程方式添加约束的示例,

ConstraintLayout mConstraintLayout  = (ConstraintLayout)fndViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();

ImageView view = new ImageView(this);
mConstraintLayout.addView(view,0);
set.clone(mConstraintLayout);
set.connect(view.getId(), ConstraintSet.TOP, mConstraintLayout.getId(), ConstraintSet.TOP, 60);
set.applyTo(mConstraintLayout); 

要了解更多详情,请参阅Constraint layout