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

在这里插入图片描述
设置宽高比
这在以前的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" />

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

上面代码中的宽高不起任何作用,只需要设置idapp:constraint_referenced_ids即可。如果group1.setVisibility(View.GONE);那么button3textView2将同时消失,很方便。

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

参考资料

ConstraintLayout 完全解析

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值