我在ConstrainLayout中有一个视图如下.
我想根据某些条件将视图更改为app:layout_constraintLeft_toLeftOf="@+id/parent"
或layout_constraintLeft_toRightOf="@+id/parent"
在recycleViewHolder中以编程方式.
以下是使用java代码将按钮设置到父视图底部的示例:
ConstraintLayout constraintLayout; ConstraintSet constraintSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); constraintLayout = (ConstraintLayout) findViewById(R.id.activity_main_constraint_layout); Button button = new Button(this); button.setText("Hello"); constraintLayout.addView(button); constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); constraintSet.connect(button.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.RIGHT, 0); constraintSet.constrainDefaultHeight(button.getId(), 200); constraintSet.applyTo(constraintLayout); }
实现这样的目标,
app:layout_constraintLeft_toLeftOf="@+id/parent"
你的java代码应该是这样的
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
并实现这样的目标,
layout_constraintLeft_toRightOf="@+id/parent"
你的java代码应该是这样的,
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
在这里,我假设android:id="@+id/parent"
是您的父ConstraintLayout的id.