Android ConstraintLayout

本文详细介绍了ConstraintLayout的特点及其在Android开发中的应用。ConstraintLayout通过约束方式指定控件位置,有效减少布局嵌套,提升性能。文章列举了常用的约束属性,并解释了如何实现居中布局、设置权重、尺寸限制等高级功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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_toRightOf
layout_constraintStart_toStartOf :同left_toLeftOf
layout_constraintEnd_toStartOf :同right_toLeftOf
layout_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_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_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 (用逗号分隔的前方约束的高度);既是高度已经指定大小,宽度按照对应比例分配大小;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值