ContraintLayout布局的约束性:
约束布局,控件之间,控件与父布局之间具有约束关系,控件的位置是按照约束来计算的;子view通过一个个约束的属性,来决定自己的位置;
RelativeLayout可以实现对控件的约束,实现起来相对简单;而LinearLayout虽然使用简单,但是会加深布局深度层级,影响app的解析效率问题;
ContraintLayout的其他属性和relativelayout基本一致,同时ContraintLayout可以约束控件的比例(LinearLayout中的weight属性)
设置居中和bias:
居中的方式可以使用左边和右边两个方向同时约束来定位布局的位置:
<android.support.constraint.ConstraintLayout ...>
<Button android:id="@+id/button" ...
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent/>
</>
bias:
可以使用bias进行控件的偏移设置:
<android.support.constraint.ConstraintLayout ...>
<Button android:id="@+id/button" ...
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent/>
</>
控件尺寸约束:在ConstraintLayout中设置控件的宽度和高度是通过Android:layout_width和Android:layout_height来指定,但是有三种不同类型:
1.使用确定的尺寸,给定数值,比如36dp
2.使用wrap_content,该效果与其他控件相同
3.使用0dp来表示match_constraint,意思是根据约束规则制定宽度和高度(根match_parent效果是相同的)
设置控件的宽高比:
Layout_constraintDimensionRatio限制宽高比,如果使用宽高比来约束尺寸,则至少要设置宽高其中的一个尺寸为0dp,然后在设置上layout_contraintDimensionRatio属性;
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />