关于ConstraintLayout的部分属性总结(强化版RelativeLayout,包含LinearLayout的比例属性,减少层级利器)
2017-03-07 10:04
686 查看
一、使用方式:
Android studio 2.2以上并添加依赖 compile ‘com.android.support.constraint:constraint-layout:1.0.1’
二、相对位置属性如下:
layout_constraintLeft_toLeftOf :当前View的右侧和另一个View的右侧位置对齐,与RelativeLayout的alignLeft属性相似layout_constraintLeft_toRightOf :当前view的左侧会在另一个View的右侧位置 与RelativeLayout的toRightOf属性相似
layout_constraintRight_toLeftOf :当前view的右侧会在另一个View的左侧位置 与RelativeLayout的toLeftOf属性相似
layout_constraintRight_toRightOf :当前View的右侧和另一个View的右侧位置对其,与RelativeLayout的alignRight属性相似
layout_constraintTop_toTopOf :头部对齐,与alignTop相似
layout_constraintTop_toBottomOf :当前View在另一个View的下侧 与below相似
layout_constraintBottom_toTopOf :当前View在另一个View的上方 与above相似
layout_constraintBottom_toBottomOf :底部对齐,与alignBottom属性相似
layout_constraintBaseline_toBaselineOf :文字底部对齐,与alignBaseLine属性相似
layout_constraintStart_toEndOf :同left_toRightOf
layout_constraintStart_toStartOf :同left_toLeftOf
layout_constraintEnd_toStartOf :同right_toLeftOf
layout_constraintEnd_toEndOf :同right_toRightOf
三、Margins属性:同RelativeLayout属性
android:layout_marginStartandroid:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
四、Margins when connected to a Gone widget
[b]当前View与另一个View绑定后,另一个View的属性设置为了Gone,则以下属性会生效[/b]layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom
五、center position and bias
[b]居中并设置权重,使view居中并且设置权重,同RelativeLayout的center_horizontal/vertical=“true” [/b]设置方法:以横向居中为例:
将ConstraintLayout的子View的属性如下进行设置
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello,World" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
将宽度设置为wrap_content并设置两个属性:
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
即可将该View横向居中显示。(竖向同理)。
利用bias设置权重:
设置居中之后默认的权重的为0.5。即正中央显示,效果如图
app:layout_constraintHorizontal_bias="0.3"
可以通过设置app:layout_constraintHorizontal_bias属性进行位置的调整,表示距离左侧(因为是横向,纵向则是距离顶部)30%的的距离。
六、Dimension constraints
android:minWidth set the minimum width for the layoutandroid:minHeight set the minimum height for the layout
[b]当子View的宽/高设置为wrap_content时,会用到minWidth和minHeight这两个属性[/b]
七、Widget dimension constraints
//使用一个具体的值Using a specific dimension (either a literal value such as 123dp or a Dimension reference)
//使用wrap_content让控件自己来计算大小
Using WRAP_CONTENT, which will ask the widget to compute its own size
//用0dp来指定,意思就是Match_Constraint
Using 0dp, which is the equivalent of “MATCH_CONSTRAINT”
注意:
ConstraintLayout 不支持match_parent属性,但支持wrap_content属性。如果你需要用match_parent,将宽度/高度指定为0dp,然后设置left_toleft,right_toRight为parent即可实现横向充满,同理设置竖向的
八、Ratio比例大小属性
当你的父控件为ConstraintLayout,可以利用这个属性来控制当前View的宽高比。在利用这个属性时,你必须指明一个方向上的大小为0dp,另一个方向可指定为明确的dp值也可以使用wrap_content这样才能够按照比例来为你摆放
<ImageView android:layout_width="100dp" android:layout_height="0dp" android:src="@mipmap/ic_launcher" app:layout_constraintDimensionRatio="1:4" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />
通过指定 app:layout_constraintDimensionRatio="1:1" 属性来指定控件宽高的比,默认为宽:高
你也可以通过下面的方法进行设置:
<ImageView android:layout_width="0dp" android:layout_height="0dp" android:src="@mipmap/ic_launcher" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintDimensionRatio="H,3:1" app:layout_constraintTop_toTopOf="parent" />
上面的这种情况将宽和高均指定为了0dp,但是通过top_toTopOf bottom_toBottomOf 两个属性指定了在高度上的大小。另外通过在ratio属性中指明的H,是为了告诉ConstraintLayout已经指定了高度上的大小。而并非是指定比例为高:宽
九、Chains
chains:锁链,通过设置属性可以将一个方向上的控件形成锁链(相互依赖),并且能够实现比例分布(类似于LinearLayout的weight分布)
形成chains的条件:
head(即chains的第一个view)必须包含top_toTopOf或者left_toLeftOf
chain的最后一个View必须指定bottom_toBottomOf或者right_toRightOf
chain中的View必须互相依赖
如:view a 和view b在竖直方向上形成锁链: a的属性设置为 top_toTopOf = “parent” bottom_toTopOf = “b” b的属性设置为top_toBottomOf=“a” bottom_toBottom = “parent"
形成依赖的好处:可以使用比例进行设置(使用方式同LinearLayout的weight属性,效果也相同)
chains Style(chains 样式)
提供了三种默认的样式:spread、packed、spread_inside
spread样式:
spread_inside样式:
包含weight属性的chain
packed样式
有bias的packed样式
十、ConstraintSet类:
这个类是辅助进行代码中动态设置ConstraintSet属性的。
具体文档参见官方API
十一、GuideLine类:
官方文档:Widgets can then be constrained to a Guideline, allowing multiple widgets to be positioned easily from one Guideline, or allowing reactive layout behavior by using percent positioning. GuideLine类是一个辅助的导航线类,默认是不显示的,只提供一个占位符以供其他View进行约束。(可通过android:orientation指定线的方向)
GuideLine一共有3种指定的方式:
specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin) //在距离顶部或者左侧一定距离的地方放置一条导航线
specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end) //在距离右侧或者底部一定距离的地方放置一条导航线
specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)//按照父视图的百分比放置一条导航线
相关文章推荐
- 关于ConstraintLayout的部分属性总结(强化版RelativeLayout,包含LinearLayout的比例属性,减少层级利器)
- 关于ConstraintLayout的部分属性总结
- 关于android LinearLayout的比例布局
- 关于相对布局RelativeLayout的各种属性介绍
- 关于android LinearLayout的比例布局
- 关于相对布局RelativeLayout的各种属性介绍
- 关于相对布局RelativeLayout的各种属性介绍
- Android中RelativeLayout中各个属性的用法总结
- LinearLayout和RelativeLayout 属性
- 【android学习笔记】关于相对布局RelativeLayout的各种属性介绍
- 【android学习笔记】关于相对布局RelativeLayout的各种属性介绍
- android relativelayout属性总结
- LinearLayout和RelativeLayout 属性对比
- 关于相对布局RelativeLayout的各种属性介绍
- RelativeLayout布局,以及部分常用属性介绍scaleType/gravity/layout_gravity
- 【android学习笔记】关于相对布局RelativeLayout的各种属性介绍
- 关于相对布局RelativeLayout的各种属性介绍
- 【android学习笔记】关于相对布局RelativeLayout的各种属性介绍
- 关于android LinearLayout的比例布局(转载)
- LinearLayout和RelativeLayout 属性对比