// 横向:
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintEnd_toEndOf=“parent”;
// 竖向:
app:layout_constraintTop_toTopOf=“parent”;
app:layout_constraintBottom_toBottomOf=“parent”;
- 利用权重实现均分(weight属性)
- 均分时,长或宽都必须为0。
代码如下所示。
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
/*
核心代码
*/
<Button
android:id="@+id/btn9"
android:layout_width=“0dp”
android:layout_height=“0dp”
android:text=“btn9”
app:layout_constraintBottom_toBottomOf="@id/btn8"
app:layo 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》开源 ut_constraintEnd_toEndOf=“parent”
app:layout_constraintHorizontal_weight=“1”
app:layout_constraintStart_toEndOf="@+id/btn8"
app:layout_constraintTop_toTopOf="@+id/btn8"
app:layout_constraintVertical_weight=“1” />
</androidx.constraintlayout.widget.ConstraintLayout>
- 场景3:ConstraintSet的动画效果实现
代码实现如下。
ConstraintSet constraintSet=newConstraintSet();//创建ConstraintSet
Button button=newBu Android开源项目《ali1024.coding.net/public/P7/Android/git》 tton(MainActivity.this);//在其中添加一个Button
//此处的constraintLayout为布局中ConstraintLayout的一个id
constraintLayout.addView(button);
constraintSet.clone(constraintLayout);
constraintSet.constrainWidth(button.getId(),ConstraintLayout.LayoutParams.WRAP_CONTENT);
constraintSet.constrainHeight(button.getId(),ConstraintLayout.LayoutParams.WRAP_CONTENT);
constraintSet.connect(button.getId(),ConstraintSet.END,ConstraintSet.PARENT_ID,ConstraintSet.END);
constraintSet.connect(button.getId(),ConstraintSet.START,
ConstraintSet.PARENT_ID,ConstraintSet.START);
//这个按钮距离顶部的margin值为1000
constraintSet.connect(button.getId(),ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP,1000);
constraintSet.connect(button.getId(),ConstraintSet.BOTTOM,ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM);
constraintSet.applyTo(constraintLayout);
根据上述代码,ConstraintSet通过如下流程完成布局中增加一个按钮的效果:
- 创建ConstraintSet对象;
- clone(复制原布局中ConstraintLayout参数);
- 设置constrainWidth与constrainHeight(设置新控件宽高是wrap_content还是match_parent);
- 调用connect方法(设置新控件与其他控件的约束关系);
最后,如果大伙有什么好的学习方法或建议欢迎大家在评论中积极留言哈,希望大家能够共同学习、共同努力、共同进步。
小编在这里祝小伙伴们在未来的日子里都可以 升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰!!
不论遇到什么困难,都不应该成为我们放弃的理由!
很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。