您的位置:首页 > 大数据 > 人工智能

关于ConstraintLayout的部分属性总结(强化版RelativeLayout,包含LinearLayout的比例属性,减少层级利器)

2017-03-06 22:44 369 查看
使用方式:

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_marginStart

android:layout_marginEnd

android:layout_marginLeft

android:layout_marginTop

android:layout_marginRight

android:layout_marginBottom



Margins when connected to a Gone widget

当前View与另一个View绑定后,另一个View的属性设置为了Gone,则以下属性会生效

layout_goneMarginStart

layout_goneMarginEnd

layout_goneMarginLeft

layout_goneMarginTop

layout_goneMarginRight

layout_goneMarginBottom




center position and bias  居中并设置权重

使view居中并且设置倾向,同RelativeLayout的center_horizontal/vertical=“true” 
当一个ConstraintLayout的子View 宽和高均设置为wrap_content&位置设置为layout_constraintLeft_toLeftOf=“parent” layout_constraintRight_toRightOf=“parent”时,会将当前View横向居中显示.
       同理top_toTopOf=“parent” & bottom_toBottomOf=“parent”会将当前view竖向居中显示。
 bias即设置位置权重:layout_constraintHorizontal_bias=“0.3”即在横向上,放置在父控件30%的地方。同样有layout_constraintVertical_bias的属性。


Dimension constraints

android:minWidth
 set the minimum width for the layout
android:minHeight
 set the minimum height for the layout
 当子View的宽/高设置为wrap_content时,会用到minWidth和minHeight这两个属性


Widget dimension constraints

Using a specific dimension (either a literal value such as 
123dp
 or a 
Dimension
 reference)      //使用一个具体的值
Using 
WRAP_CONTENT
, which will ask the widget to compute its own size                                    //使用wrap_content让控件自己来计算大小
Using 
0dp
, which is the equivalent of “
MATCH_CONSTRAINT
”                                                       //用0dp来指定,意思就是Match_Constraint

Important: 
MATCH_PARENT
 is not supported for widgets contained in a 
ConstraintLayout
, though similar behavior can be defined by using 
MATCH_CONSTRAINT
 with the corresponding
left/right or top/bottom constraints being set to 
"parent"
.
     ConstraintLayout 不支持match_parent属性,但支持wrap_content属性。如果你需要用match_parent,将宽度/高度指定为0dp,然后设置left_toleft,right_toRight为parent即可实现横向充满,同理设置竖向的


Ratio 比例大小属性

 当你的父控件为ConstraintLayout,可以利用这个属性来控制当前View的宽高比。在利用这个属性时,你必须指明一个方向上的大小为0dp,另一个方向可指定为明确的dp值也可以使用wrap_content这样才能够按照比例来为你摆放
 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必须包含top_toTopOf或者left_toLeftOf  而chain的最后一个View必须指定bottom_toBottomOf或者right_toRightOf
 如: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属性的。
https://developer.android.com/reference/android/support/constraint/ConstraintSet.html 
官方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
)//按照父视图的百分比放置一条导航线
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息