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

原文链接https://blog.csdn.net/u013187628/article/details/60751812

一、使用方式:

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的属性如下进行设置

    <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 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)
  • //使用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)//按照父视图的百分比放置一条导航线

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 LinearLayout 是一个线性布局,可以通过设置它的方向属性(orientation)来指定子视图的排列方向(水平或垂直)。常用的属性有:gravity(设置子视图的对齐方式)、weight(设置子视图的占比权重)、layout_margin(设置子视图的外边距)、layout_weight(在子视图中设置权重比例)等。 TableLayout 是一个表格布局,可以通过定义行(TableRow)和列来布置子视图。常用的属性有:stretchColumns(设置哪些列可以被拉伸以填充空白)、shrinkColumns(设置哪些列可以被缩小以适应窗口大小)等。 RelativeLayout 是一个相对布局,可以通过指定子视图之间的相对位置来布置它们。常用的属性有:layout_alignParentTop、layout_alignParentBottom、layout_alignParentLeft、layout_alignParentRight(将子视图与父视图的顶部、底部、左边、右边对齐)、layout_toLeftOf、layout_toRightOf(将子视图放在另一个子视图的左边或右边)等。 FrameLayout 是一个帧布局,可以将子视图放在屏幕上的任意位置。常用的属性有:layout_gravity(设置子视图的位置)、foreground(设置前景色,即子视图前面的颜色)、background(设置背景色,即子视图后面的颜色)等。 GridLayout 是一个网格布局,可以将子视图放在网格中的任意位置。常用的属性有:columnCount、rowCount(指定列数和行数)、layout_column(指定子视图所在的列)、layout_row(指定子视图所在的行)等。 ConstraintLayout 是一个约束布局,可以通过设置子视图之间的约束关系来布置它们。常用的属性有:layout_constraintLeft_toLeftOf、layout_constraintRight_toRightOf、layout_constraintTop_toTopOf、layout_constraintBottom_toBottomOf(将子视图与其他子视图或父视图的边缘对齐)、layout_constraintHorizontal_weight、layout_constraintVertical_weight(在水平或垂直方向上设置子视图的权重)等。 以上就是这些布局的常用属性及作用。希望能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值