ConstraintLayout, 即约束布局, 在2016年由Google I/O推出. 从支持力度而言, 将成为主流布局样式, 完全代替其他布局, 减少布局的层级, 优化渲染性能. 在新版Android Studio中, ConstraintLayout已替代RelativeLayout, 成为HelloWorld项目的默认布局. ConstraintLayout作为非绑定(Unbundled)的支持库, 命名空间是app:, 即来源于本地的包命名空间. 最新版本是1.0.1(2017.4.21), 在项目的build.gradle中声明.
dependencies { compile 'com.android.support.constraint:constraint-layout:1.0.2' }ConstraintLayout中0代表:MATCH_CONSTRAINT,这个布局通过约束去控制UI的显示,而不是通过设定view的大小
spread width=0 此种情况下即是例子中的③视图,可以通过weight来设定比例,默认即是spread
spread width != 0
<Button app:layout_constraintHorizontal_chainStyle="spread" android:id="@+id/btn1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/black" app:layout_constraintRight_toLeftOf="@id/btn2" android:layout_width="30dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintLeft_toRightOf="@+id/btn1" android:id="@+id/btn2" app:layout_constraintRight_toLeftOf="@id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_green_dark" android:layout_width="40dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintLeft_toRightOf="@+id/btn2" android:id="@+id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_red_dark" app:layout_constraintRight_toRightOf="parent" android:layout_width="60dp" android:layout_height="wrap_content"/> spread_inside + 宽度非0 <Button app:layout_constraintHorizontal_chainStyle="spread_inside" android:id="@+id/btn1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/black" app:layout_constraintRight_toLeftOf="@id/btn2" android:layout_width="30dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="spread_inside" app:layout_constraintLeft_toRightOf="@+id/btn1" android:id="@+id/btn2" app:layout_constraintRight_toLeftOf="@id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_green_dark" android:layout_width="40dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="spread_inside" app:layout_constraintLeft_toRightOf="@+id/btn2" android:id="@+id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_red_dark" app:layout_constraintRight_toRightOf="parent" android:layout_width="60dp" android:layout_height="wrap_content"/> packed + width非0 <Button app:layout_constraintHorizontal_chainStyle="packed" android:id="@+id/btn1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/black" app:layout_constraintRight_toLeftOf="@id/btn2" android:layout_width="30dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toRightOf="@+id/btn1" android:id="@+id/btn2" app:layout_constraintRight_toLeftOf="@id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_green_dark" android:layout_width="40dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toRightOf="@+id/btn2" android:id="@+id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_red_dark" app:layout_constraintRight_toRightOf="parent" android:layout_width="60dp" android:layout_height="wrap_content"/> packed + bias <Button app:layout_constraintHorizontal_bias="0.3"//左偏30%宽度 app:layout_constraintHorizontal_chainStyle="packed" android:id="@+id/btn1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/black" app:layout_constraintRight_toLeftOf="@id/btn2" android:layout_width="30dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toRightOf="@+id/btn1" android:id="@+id/btn2" app:layout_constraintRight_toLeftOf="@id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_green_dark" android:layout_width="40dp" android:layout_height="wrap_content"/> <Button app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toRightOf="@+id/btn2" android:id="@+id/btn3" app:layout_constraintTop_toBottomOf="@+id/icon_head" android:background="@android:color/holo_red_dark" app:layout_constraintRight_toRightOf="parent" android:layout_width="60dp" android:layout_height="wrap_content"/>动态添加一个button
ConstraintLayout mRootLy = findViewById(R.id.root_ly); ConstraintSet constraintSet = new ConstraintSet(); mDynBtn = new Button(TestAACActivity.this); mDynBtn.setId(R.id.dyn_btn); //添加view mRootLy.addView(mDynBtn); //克隆set constraintSet.clone(mRootLy); constraintSet.constrainWidth(mDynBtn.getId(), ConstraintLayout.LayoutParams.WRAP_CONTENT); constraintSet.constrainHeight(mDynBtn.getId(), ConstraintLayout.LayoutParams.WRAP_CONTENT); //放在父布局的底部 constraintSet.connect(mDynBtn.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM); //放在mCurrStatusTv的右边,并且margin=30px constraintSet.connect(mDynBtn.getId(), ConstraintSet.LEFT, mCurrStatusTv.getId(), ConstraintSet.RIGHT, 30); //和mAddRemoveBtn上对齐 constraintSet.connect(mDynBtn.getId(), ConstraintSet.TOP, mAddRemoveBtn.getId(), ConstraintSet.TOP); //应用约束 constraintSet.applyTo(mRootLy);移除view
mRootLy.removeView(mRootLy.findViewById(R.id.dyn_btn));