ContraintLayout布局总结

总结

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" />

ContraintLayout布局总结_第1张图片
设置宽高比
这在以前的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
支持:spreadspread_insidepacked
在这里插入图片描述

    <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" />

实现LinearLayout Weight效果
排布样式
使用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" />

总体下来constraintLayout功能很强大,而且能有效减少布局的嵌套,使用起来也不复杂。目前,已是layout的默认选项。

参考资料

ConstraintLayout 完全解析

你可能感兴趣的:(Android学习,Android笔记,android)