栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

约束布局ConstraintLayout总结,腾讯架构师深入讲解Android开发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

约束布局ConstraintLayout总结,腾讯架构师深入讲解Android开发

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: 代表约束的半径大小

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

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

android:text=“tv2”

app:layout_constraintCircle="@id/tv1"

app:layout_constraintCircleAngle=“45”

app:layout_constraintCircleRadius=“100dp” />

尺寸约束


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

使用指定的尺寸

使用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_constraintDimensionRati

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享

o设置宽高比

总结

可以看出,笔者的工作学习模式便是由以下 「六个要点」 组成:

❝ 多层次的工作/学习计划 + 番茄工作法 + 定额工作法 + 批处理 + 多任务并行 + 图层工作法❞

希望大家能将这些要点融入自己的工作学习当中,我相信一定会工作与学习地更富有成效。

下面是我学习用到的一些书籍学习导图,以及系统的学习资料。每一个知识点,都有对应的导图,学习的资料,视频,面试题目。

**如:我需要学习 **Flutter的知识。(大家可以参考我的学习方法)

  • Flutter 的思维导图(无论学习什么,有学习路线都会事半功倍)

  • Flutter进阶学习全套手册

  • Flutter进阶学习全套视频

大概就上面这几个步骤,这样学习不仅高效,而且能系统的学习新的知识。

自己的工作学习当中,我相信一定会工作与学习地更富有成效。

下面是我学习用到的一些书籍学习导图,以及系统的学习资料。每一个知识点,都有对应的导图,学习的资料,视频,面试题目。

**如:我需要学习 **Flutter的知识。(大家可以参考我的学习方法)

  • Flutter 的思维导图(无论学习什么,有学习路线都会事半功倍)

[外链图片转存中…(img-yu5rQ69D-1640330020451)]

  • Flutter进阶学习全套手册

[外链图片转存中…(img-iWHUWmYa-1640330020495)]

  • Flutter进阶学习全套视频

[外链图片转存中…(img-YI6zrwDo-1640330020495)]

大概就上面这几个步骤,这样学习不仅高效,而且能系统的学习新的知识。

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/679171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号