Android ConstraintLayout

来源:互联网 发布:大牌护手霜好 知乎 编辑:程序博客网 时间:2024/05/19 00:15

constraintLayout和RelativeLayout类似,但比RelativeLayout要强大多,它可以有效地解决布局嵌套过多问题,我们平时编写的界面,复杂的布局总会伴随着多层的嵌套,而嵌套越多,程序的性能也就越差;ConstraintLayout则是使用约束的方式来指定各个控件的位置和关系;

2.常用相对属性:

layout_constraintTop_toTopOf       // 将所需视图的顶部与另一个视图的顶部对齐。 layout_constraintTop_toBottomOf    // 将所需视图的顶部与另一个视图的底部对齐。 layout_constraintBottom_toTopOf    // 将所需视图的底部与另一个视图的顶部对齐。 layout_constraintBottom_toBottomOf // 将所需视图的底部与另一个视图的底部对齐。 layout_constraintLeft_toTopOf      // 将所需视图的左侧与另一个视图的顶部对齐。 layout_constraintLeft_toBottomOf   // 将所需视图的左侧与另一个视图的底部对齐。 layout_constraintLeft_toLeftOf     // 将所需视图的左边与另一个视图的左边对齐。 layout_constraintLeft_toRightOf    // 将所需视图的左边与另一个视图的右边对齐。 layout_constraintRight_toTopOf     // 将所需视图的右对齐到另一个视图的顶部。layout_constraintRight_toBottomOf  // 将所需视图的右对齐到另一个的底部。layout_constraintRight_toLeftOf    // 将所需视图的右边与另一个视图的左边对齐。layout_constraintRight_toRightOf   // 将所需视图的右边与另一个视图的右边对齐。layout_constraintBaseline_toBaselineOf :文字底部对齐,与alignBaseLine属性相似layout_constraintStart_toEndOf :同left_toRightOflayout_constraintStart_toStartOf :同left_toLeftOflayout_constraintEnd_toStartOf :同right_toLeftOflayout_constraintEnd_toEndOf :同right_toRightOf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.margins属性

android:layout_marginStartandroid:layout_marginEndandroid:layout_marginLeftandroid:layout_marginTopandroid:layout_marginRightandroid:layout_marginBottom
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

layout_goneMarginStartlayout_goneMarginEndlayout_goneMarginLeftlayout_goneMarginToplayout_goneMarginRightlayout_goneMarginBottom
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5.居中并设置权重,使view居中并且设置权重,同RelativeLayout的center_horizontal/vertical=“true”

设置方法:以横向居中为例(竖向同理): 
将ConstraintLayout的子View的属性如下进行设置

<TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

6.设置权重layout_constraintHorizontal_bias(layout_constraintVertical_bias)

例如:下面将会使左侧用30%的偏差,而不是默认的50%,使得左侧将会缩短,与微件倾斜更靠近左侧

<TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"        app:layout_constraintHorizontal_bias="0.3"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

7.尺寸限制

在ConstraintLayout最小尺寸,被使用ConstraintLayout时,其尺寸设置为WRAP_CONTENT

  • android:minWidth 设定的最小宽度为布局
  • android:minHeight 设置最低高度布局

注意:

ConstraintLayout 不支持match_parent属性,但支持wrap_content属性。如果你需要用match_parent,将宽度/高度指定为0dp,然后设置left_toleft,right_toRight为parent即可实现横向充满,同理设置竖向的

8.Ratio比例大小属性

You can also define one dimension of a widget as a ratio of the other one. In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT), and set the attribute layout_constraintDimentionRatio to a given ratio. For example:

当你的父控件为ConstraintLayout,可以利用这个属性来控制当前View的宽高比。在利用这个属性时,你必须指明一个方向上的大小为0dp,另一个方向可指定为明确的dp值也可以使用wrap_content这样才能够按照比例来为你摆放 
例如:“宽度:高度

<Button     android:layout_width="wrap_content"    android:layout_height="0dp"    app:layout_constraintDimensionRatio="1:1" />
  • 1
  • 2
  • 3
  • 4

也可以这样设置:

<ImageView        android:layout_width="0dp"        android:layout_height="0dp"        android:src="@mipmap/ic_launcher"        app:layout_constraintDimensionRatio="H,6:1"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintTop_toTopOf="parent"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

把宽和高设置为0dp,但是通过Top_toTopOf、Bottom_toBottomOf指定高度,通过添加字母W(为约束的宽度)或H (用逗号分隔的前方约束的高度);既是高度已经指定大小,宽度按照对应比例分配大小;