文章目录

  • 前言 ~
  • 应用ConstraintLayout ~
    • 简介
    • 添加gradle依赖
    • 创建ConstraintLayout为根元素的布局文件
    • 目前可使用的各种类型限制
  • Relative positioning 相对定位 ~
  • Margins ~
    • 普通的margin
  • Centering positioning and bias (居中定位和偏移) ~
    • centering positioning
  • Visibility behavior ~
  • Dimension constraints ~
    • 设置最小尺寸
    • 组件尺寸约束
    • match_constraint and ratio
  • Chains ~
    • Creating a chain
    • Chain heads
    • Margins in chains
    • Chain Style
    • Weighted chains
    • Percent chains (added in 1.1)
    • Chains 图示:
  • Virtual Helpers objects ~
    • Guideline
    • Barrier
    • Group
  • 其它

前言 ~

Google I/O 2016 上发布了 ConstraintLayout。它的优点就是能减少布局的层级,用它完全替换RelativeLayout和LinearLayout应该都是可以的。

关于ConstraintLayout的一些Android Studio界面操作可以看《Constraint 代码实验室–带你一步步理解使用 ConstraintLayout》,其英文出处可以看Using ConstraintLayout to design your views

本文主要翻译官网参考文档,并给出一些示例;还有一些与constraintLayout相关的一些元素或属性的使用。

文中基于的约束布局的版本为:constraint-layout:1.1.0-beta1 (需要使用studio3.0,目前未有正式版。像文末的Barrier和Group暂无法在正式版环境中使用)

 


应用ConstraintLayout ~

简介

ConstraintLayout 继承自ViewGroup,它提供了一种更为灵活的方式来处理组件的位置和尺寸。
ConstraintLayout作为一个support library,它最小兼容android api level 9。

添加gradle依赖

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'

创建ConstraintLayout为根元素的布局文件

<?xml version="1.0" encoding="utf-8"?>  

目前可使用的各种类型限制

  • Relative positioning
  • Margins
  • Centering positioning and bias(居中定位和偏移)
  • Visibility behavior
  • Dimension constraints
  • Chains
  • Virtual Helpers objects

 


Relative positioning 相对定位 ~

在ConstraintLayout中,可以相对于一个其它的组件,而定位当前组件。约束一个组件的横轴和纵轴,相关的属性有:

  • 横轴: Left, Right, Start and End sides
  • 纵轴:Top, Bottom sides and text baseline

关于View的Left、Top、Right、Bottom的方向好理解。Start 和 End,是指当前语言的开始和结束方向:大多数语言都是从左向右的,而少数语言是从右向左书写的,比如阿拉伯语

例如,要使button B 在 button A的右边:

如下表,列出了可用的相对位置约束属性:

available relative positioning constraints
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

 
这些属性的规则:layout_constraint[source]_to[target]Of,即source的某个方向,在target的某个方向;source表示当前组件,target即参考引用的组件

它们都可以参考一个其它组件的id,或 parent

 


Margins ~

普通的margin

可设置margin属性,列表如下:

margins
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom

注意:这里的margin值,只能是一个『正数、0 或 Dimension资源』

##当margin一个gone组件
当位置约束的target的可见性为:View.GONE时,可以使用如下表示中的属性:

gone margins
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

 


Centering positioning and bias (居中定位和偏移) ~

centering positioning

例如,

上面的示例中,宽高都固定了一个数值(或是wrap_content)。要想约束button的左面在父级的左面,右面在父级的右面, 这根本是可能的,所以系统将其解释成水平『居中』了,同理若加上app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" 则纵向也就居中了

##bias
可以在上面的基础上,加上偏移率(bias):

app:layout_constraintHorizontal_bias="0.1"app:layout_constraintVertical_bias="0.8"

效果:

h-bias的取值范围[0.0, 1.0],方向从左向右
v-bias的取值范围[0.0, 1.0],方向从上向下

 


Visibility behavior ~

链中使用 margin, 分 android:layout_margin, app:layout_goneMargin

结合前面的『margins』,来谈谈对于android:layout_margin和layout_goneMargin的区别

  • android:layout_margin
    这个无需要多言,就是非 gone 时 都有效
  • layout_goneMargin
    这个单词顺序换一下就好理解了,即 margin (链中前一个)gone 状态的组件
    详细点说:当前组件设置了goneMargin,且这时当前不为 gone 状态;在链中前一个组件为 gone 时 有效。 gone时,goneMargin对自身无效

注意:当target有其它约束属性时,会影响到source组件的定位

 


Dimension constraints ~

设置最小尺寸

当一个组件的宽或高设置为WRAP_CONTENT时,可以为它们设置一个最小尺寸:android:minWidth android:minHeight。 ConstraintLayout本身也可以设置。

组件尺寸约束

主要有三种方式:

  • 设定一个特定值,如123dp或 一个Dimension reference
  • WRAP_CONTENT,由组件自己估算它的尺寸
  • 设置成0dp,也就相当于 match_constraint(不能在xml中写上match_constraint,可以在xml界面的design视图中操作)

match_constraint and ratio

  • 内部组件的最小/最大宽高 (added in 1.1)
    layout_constraintWidth_min and layout_constraintHeight_min
    layout_constraintWidth_max and layout_constraintHeight_max

  • 当宽高至少有一项被设置为0dp时,可以使用属性 layout_constraintDimentionRatio来给定一个"宽对高"的比率。比率值是一个float值,可以写成0.5或1:2这样的形式。

例如,

而,当宽高都设置为0dp(match_constraint)时,如,

这里加载了纵向的top和bottom的约束,相对于parent,那么高度最大即parent的高度;还需要加上ratio设置,才会显示,因为此时宽度并不知道:

app:layout_constraintDimensionRatio="W, 1:4"

这时,ratio的值必须以 H或W开头,且后面跟一个逗号及数值比。

当宽和高都为match_constraint即0dp时,约束方向为x轴时(宽为parent):
w, 1:2 高比宽; h, 2:1 宽比高
约束方向为y轴时(高为parent):
w, 1:2 宽比高; h, 2:1 高比宽

 


Chains ~

chains 提供了在一个单轴(横轴或纵轴)中类似组的行为。另一轴可以独立进行约束。链(chains)中的相互两个组件之间,需要进行互相约束

Creating a chain

一个相互间双向连接的组件集合就认为是一个chain

Chain heads

chain head ,即水平chain的最左边的元素,或纵向chain的最顶部元素

Margins in chains

如果在连接中定义了margins,chain中元素的定位就会发生变化。在Chain Style为CHAIN_SPREAD时,margins设置,会扣除已分配的空间(注意,android_layoutMargin和goneMargin的表现是不同的)

Chain Style

如果在chain head元素上设置 layout_constraintHorizontal_chainStylelayout_constraintVertical_chainStyle ,chain的行为就会发生相应的变化。

styles:

  • CHAIN_SPREAD
    对应属性值:spread。默认样式,元素铺展开
    在当前样式下,如果一些组件设置了"match_constraint",它们将会分割占用有效的空间

  • CHAIN_SPREAD_INSIDE
    对应属性值:spread_inside。类似,spread。chain的两端元素不会被展开

  • CHAIN_PACKED
    对应属性值:packed。将chain中元素包裹起来,当设置了横向或纵向的bias时,会影响其中的所有元素。

如下代码,只有style=packed时,才设置bias,其它style,去除bias设置,可查看不同的效果:

      

Weighted chains

类似LinearLayout中的android:layout_weight属性。使用layout_constraintHorizontal_weight或layout_constraintVertical_weight,当宽或高为MATCH_CONSTRAINT(即等于0dp)时,起作用。

如下例子描述了,两个view宽度充满全屏,高度各种一全屏的一半:

  

Percent chains (added in 1.1)

链中组件宽或高设为0dp 时,可以使用百分比定义尺寸。范围0~1的小数。layout_constraintWidth_percent
layout_constraintHeight_percent

Chains 图示:

chains 中的相邻组件要注意 互相约束
 


Virtual Helpers objects ~

虚拟帮助对象,目前最新版1.1.0-beta1有Guideline、Barrier、Group三种对象。既然是虚拟对象,那么它们就不占用实际的空间。

Guideline

Guideline,引导线。当组件约束,使用Guideline为target时,以帮助定位组件。

Guideline主要属性有android:orientation、layout_constraintGuide_begin、layout_constraintGuide_end、layout_constraintGuide_percent

当方向指定为横向时,可帮助组件进行纵向定位;反之,纵向时,可帮助组件进行横向定位

Barrier

Barrier,直译为障碍、屏障。在约束布局中,可以使用属性constraint_referenced_ids来引用多个带约束的组件,从而将它们看作一个整体。

 

上面的TextView会在button1和button2这个整体的下面。如果将app:constraint_referenced_ids="button1, button2"中的button2去掉,会发现,TextView会仅在button1的下面

Group

Group,分组。用法跟Barrier类似,也是使用属性constraint_referenced_ids引用多个约束组件,从而分成一组。
前面说Barrier是障碍物,即表示所引用的组件,不会消失,即使设置了android:visibility="gone";而Group会消失

 


其它

TextView或Button这样的含有文本的组件。对其baseline添加约束,可以使用属性layout_constraintBaseline_toBaselineOf

 

更多相关文章

  1. ImageView的scaletype属性
  2. Android查看系统信息
  3. 使用组件构建Android应用程序
  4. Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
  5. ANDROID 【GRIDVIEW】 DETAILED ANNOTATION
  6. Android之Audio常用属性变量
  7. android 之 Attr的使用
  8. Android(安卓)TextView中android:textIsSelectable属性在Recycle
  9. android圆形的ImageView

随机推荐

  1. 固定导航栏|可编辑表格
  2. Anaconda环境配置
  3. Rocky Linux镜像在阿里云镜像站首发上线
  4. 自定义方法通过类名获取对象集合
  5. 一套有效应对技术面算法题的方法论
  6. Apache服务器是如何解析PHP 小编来给你解
  7. 2021年校招程序员之阿里的十轮面试问题真
  8. PHP高并发高可用系统以及面试分析
  9. Python(十)文件操作
  10. Linux SRE 必经之路