总结
ConnstraintLayout
对于减少布局嵌套,提升性能很有帮助。
通过这些属性,可以实现和RelativeLayout相似的效果。
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
需要注意,他不想RelativeLayout 宽度会随着约束改变。ConnstraintLayout
的宽度是不变的,如果想实现约束边界的宽度,可以指定宽度为0
. 这样就可以达到和RelativeLayout
一样的效果.
RelativeLayout实现不了的
实现按比例排布
layout_constraintHorizontal_bias
实现左右间距的比例不同。
<TextView
android:id="@+id/tvBias"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="我是按比例排布,左边2份,右边8份"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvInCenter" />
设置宽高比
这在以前的layout是不可能做到的,constrantLayout
实现起来比较简单。
<TextView
android:id="@+id/tvRatio"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="30dp"
android:background="#3000ffff"
android:gravity="center"
android:text="这是一个宽高比为16:9的TextView"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvBias" />
实现权重大小
如果是等比1:1,使用上面的就够了,如果使用比例使用
相当于实现LinearLayout Weight效果
layout_constraintHorizontal_weight
来控制权重。实现LinearLayout
的特性。
还可以,使用chainStyle
设置不同的展示样式,默认spread
支持:spread
,spread_inside
,packed
<TextView
android:id="@+id/tvButton1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#66ff0000"
android:padding="4dp"
android:text="我占2份"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tvButton2"
/>
<TextView
android:id="@+id/tvButton2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#6600ff00"
android:padding="4dp"
android:text="我占1份"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/tvButton1"
app:layout_constraintRight_toLeftOf="@+id/tvButton3" />
<TextView
android:id="@+id/tvButton3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#660000ff"
android:padding="4dp"
android:text="我占1份"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/tvButton2"
app:layout_constraintRight_toRightOf="parent" />
排布样式
使用layout_constraintHorizontal_chainStyle
辅助线Guideline
只是用来辅助布局,并不占位。
常用的几个属性layout_constraintGuide_percent
, layout_constraintGuide_begin
layout_constraintGuide_end
还有orientation
来确定是横向还是纵向。
<androidx.constraintlayout.widget.Guideline
android:id="@+id/gl_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="600dp" />
Group组
Group
也是一个辅助类,不会绘制到屏幕上,也不会展现给用户。
Group
通过属性app:constraint_referenced_ids
将一些View组成组进行集体操作,最常见的操作是setVisibility
。
<android.support.constraint.Group
android:id="@+id/group1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="button3, textView2"/>
上面代码中的宽高不起任何作用,只需要设置id
和app:constraint_referenced_ids
即可。如果group1.setVisibility(View.GONE)
;那么button3
和textView2
将同时消失,很方便。
总体下来,constraintLayout
功能很强大,而且能有效减少布局的嵌套,使用起来也不复杂。目前,已是layout
的默认选项。