给寻找编程代码教程的朋友们精选了相关的编程文章,网友曹婧妍根据主题投稿了本篇教程内容,涉及到Android、ConstraintLayout、布局约束、Android、ConstraintLayout修改辅助、Android ConstraintLayout 布局约束相关内容,已被844网友关注,相关难点技巧可以阅读下方的电子资料。
Android ConstraintLayout 布局约束
实践过程
代码修改约束
public class ActivityJavaConstraintLayout extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_java_constraint); ConstraintLayout idConstraintLayout = findViewById(R.id.idConstraintLayout); findViewById(R.id.idBtn).setOnClickListener(v -> { ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(idConstraintLayout); //设置相对父布局剧中 constraintSet.centerHorizontally(R.id.idTxt, ConstraintSet.PARENT_ID); constraintSet.centerVertically(R.id.idTxt, ConstraintSet.PARENT_ID); //可以添加动画 TransitionManager.beginDelayedTransition(idConstraintLayout); constraintSet.applyTo(idConstraintLayout); }); //还有其他形式 控件A底部和空间B顶部对其 等同于xml中属性 app:layout_constraintBottom_toTopOf // constraintSet.connect(R.id.idTxtA, ConstraintSet.BOTTOM, R.id.idTxtB, ConstraintSet.TOP); } }
除了上面的效果,我们还能实现切换布局的时候,相同控件动画化。我们准备两个布局,布局内id一致,但是位置不同。
布局A如下:
布局B如下:
public class ActivityJavaConstraintLayout extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_java_constraint); ConstraintLayout idConstraintLayout = findViewById(R.id.idConstraintLayout); //变化 findViewById(R.id.idBtn).setOnClickListener(v ->{ ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(this,R.layout.activity_java_constraint_b); TransitionManager.beginDelayedTransition(idConstraintLayout); constraintSet.applyTo(idConstraintLayout); }); //恢复 findViewById(R.id.idBtnBack).setOnClickListener(v->{ ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(this,R.layout.activity_java_constraint); TransitionManager.beginDelayedTransition(idConstraintLayout); constraintSet.applyTo(idConstraintLayout); }); } }