以编程方式添加约束

cod*_*ler 8 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"
Run Code Online (Sandbox Code Playgroud)

Kul*_*uls 13

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

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); 
Run Code Online (Sandbox Code Playgroud)

要了解更多详细信息,可以参考约束布局