约束布局ConstraintLayout总结

ConstraintLayout是目前android官方比较推荐的布局方式,Android进行布局时减少对布局层次的嵌套,进而提高app的性能。

约束顾名思义,就是将该view约束到指定的view上(包括外层的ConstraintLayout和其他的布局),实现位置的定位。

因此有时候可能需要更多的定位去约束控件的位置,强大的功能,灵活的使用给上手带来了一点点的难度,很多开发者望而生畏。其实掌握之后,真的越用越舒服!!!

使用


ConstraintLayout的使用需要添加依赖,

implementation ‘com.android.support.constraint:constraint-layout:1.1.3’

和relativelayout相似的功能


app:layout_constraintStart_toStartOf=“@id/view”

app:layout_constraintLeft_toLeftOf=“@id/view”

app:layout_constraintEnd_toEndOf=“@id/view”

app:layout_constraintRight_toRightOf=“@id/view”

app:layout_constraintTop_toTopOf=“@id/view”

app:layout_constraintBaseline_toBaselineOf=“@id/view”

app:layout_constraintBottom_toBottomOf=“@id/view”

app:layout_constraintStart_toEndOf=“@id/view”

app:layout_constraintLeft_toRightOf=“@id/view”

app:layout_constraintEnd_toStartOf=“@id/view”

app:layout_constraintRight_toLeftOf=“@id/view”

app:layout_constraintTop_toBottomOf=“@id/view”

app:layout_constraintBottom_toTopOf=“@id/view”

望名知意,这些的意思是把约束控件的 上下左右 放到提供约束的控件的 的 上下左右 位置上。

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintBottom_toBottomOf=“parent”

要想实现水平居中只需设置left与right分别约束parent,而要想实现竖直居中则只需设置top与bottom分别约束parent

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

layout_constraintBaseline_toBaselineOf

Baseline指的是文本基线,即使两个不同宽高的textview,可以把文字基线对齐,即文字对齐。

Bias(偏向)

当遇到两个相反方向的约束时,默认情况下控件是居中的;然而要想让控件偏向某一边,可以使用bias属性:

layout_constraintHorizontal_bias 水平偏移

layout_constraintVertical_bias 垂直偏移

Bias的大小在0-1之间,等比例进行偏移,适配的时候很好用。

LinearLayout 相似功能


这两种代表偏移的方向

app:layout_constraintHorizontal_chainStyle=“”

app:layout_constraintVertical_chainStyle=“”

spread_inside:两边不留空间,中间间距平分

spread:完全均分

packed:完全不留间距

在这里插入图片描述

需要注意的是:要达到上的的chain效果,他们之间必须完全相互约束,同时chain style的设置都是以第一个view为基点。同时默认chain style为spread。

app:layout_constraintHorizontal_weight设置权重

可以通过设置权重,进行控件尺寸的分配

margin


1.Margin 作用对象 : 如果某一边对应的 Margin 属性被设置 , 这些 Margin 会被应用于对应的约束 ;

2.Margin 作用效果 : 设置 Margin 属性会强制在 被约束的边 ( 源边 Source Side ) 和 约束到的位置 之间 填充指定尺寸的空间 ;

3.Margin 属性类型及取值范围 : Margin 属性值是尺寸类型 , 设置 dip , px 等数值属性 , 其值必须大于等于0

4、padding不受影响

goneMargin

goneMargin主要用于约束的控件可见性被设置为gone的时候使用的margin值,属性如下:

ayout_goneMarginStart

layout_goneMarginEnd

layout_goneMarginLeft

layout_gon

MarginTop

layout_gone

MarginRight

layout_goneMarginBottom

只有用于约束的控件消失后,该属性才会生效。

Circle

layout_constraintCircle: 代表约束的view的id

layout_constraintCircleAngle: 代表约束的角度

layout_constraintCircleRadius: 代表约束的半径大小

<android.support.constraint.ConstraintLayout

xmlns:app=“http://schemas.android.com/apk/res-auto”

<TextView

android:id=“@+id/tv1”

android:text=“tv1”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

<TextView

android:text=“tv2”

app:layout_constraintCircle=“@id/tv1”

app:layout_constraintCircleAngle=“45”

app:layout_constraintCircleRadius=“100dp” />

</android.support.constraint.ConstraintLayout>

尺寸约束


控件的尺寸可以通过四种不同方式指定:

使用指定的尺寸

使用wrap_content,让控件自己计算大小

当控件的高度或宽度为wrap_content时,可以使用下列属性来控制最大、最小的高度或宽度:

android:minWidth 最小的宽度

android:minHeight 最小的高度

android:maxWidth 最大的宽度

android:maxHeight 最大的高度

使用 0dp (MATCH_CONSTRAINT)

官方不推荐在ConstraintLayout中使用match_parent,可以设置 0dp (MATCH_CONSTRAINT) 配合约束代替match_parent

例如

宽度设为0dp,左右两边约束parent的左右两边,并设置左边边距为50dp

宽高比

当宽或高至少有一个尺寸被设置为0dp时,可以通过属性layout_constraintDimensionRatio设置宽高比

app:layout_constraintDimensionRatio="H,2:3"指的是 高:宽=2:3

app:layout_constraintDimensionRatio="W,2:3"指的是 宽:高=2:3

Percent dimension(百分比维度)

layout_constraintWidth_percent

layout_constraintHeight_percent 设置为0到1之间的值

一些辅助


Optimizer

可以通过添加属性app:layout_optimizationLevel 来决定ConstraintLayout 中哪些元素需要优化

none : 不使用优化

总结

现在新技术层出不穷,如果每次出新的技术,我们都深入的研究的话,很容易分散精力。新的技术可能很久之后我们才会在工作中用得上,当学的新技术无法学以致用,很容易被我们遗忘,到最后真的需要使用的时候,又要从头来过(虽然上手会更快)。

我觉得身为技术人,针对新技术应该是持拥抱态度的,入了这一行你就应该知道这是一个活到老学到老的行业,所以面对新技术,不要抵触,拥抱变化就好了。

Flutter 明显是一种全新的技术,而对于这个新技术在发布之初,花一个月的时间学习它,成本确实过高。但是周末花一天时间体验一下它的开发流程,了解一下它的优缺点、能干什么或者不能干什么。这个时间,并不是我们不能接受的。

如果有时间,其实通读一遍 Flutter 的文档,是最全面的一次对 Flutter 的了解过程。但是如果我们只有 8 小时的时间,我希望能关注一些最值得关注的点。

(跨平台开发(Flutter)、java基础与原理,自定义view、NDK、架构设计、性能优化、完整商业项目开发等)


《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
行业,所以面对新技术,不要抵触,拥抱变化就好了。

Flutter 明显是一种全新的技术,而对于这个新技术在发布之初,花一个月的时间学习它,成本确实过高。但是周末花一天时间体验一下它的开发流程,了解一下它的优缺点、能干什么或者不能干什么。这个时间,并不是我们不能接受的。

如果有时间,其实通读一遍 Flutter 的文档,是最全面的一次对 Flutter 的了解过程。但是如果我们只有 8 小时的时间,我希望能关注一些最值得关注的点。

(跨平台开发(Flutter)、java基础与原理,自定义view、NDK、架构设计、性能优化、完整商业项目开发等)

[外链图片转存中…(img-iKnQp0Sd-1715123833488)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值