ConstraintLayout详解

1.介绍

约束布局ConstraintLayout本身就是一个ViewGroup,它用来解决布局嵌套过多的问题(嵌套越多,绘制消耗的时间越长),以灵活的方式定位/调整小部件(API 9以上均可使用),相比RelativeLayout更灵活,性能更加的出色,更好的适配屏幕大小不同的机型。Android Studio 2.3开始默认创建Activity是都是使用它。

2.使用

使用它也非常的简单,高版本的AS通常都会自动的添加它的依赖,如果未添加依赖在app/build.gradle中添加相应的依赖即可。

 implementation 'com.android.support.constraint:constraint-layout:1.1.3'
2.1 相对定位

即一个组件相对于另一个组件的约束,非常的好理解(与RelativeLayout非常的类似)。

例:要实现B在A的右侧:
在这里插入图片描述
按钮A在按钮B的右侧,也可以理解为按钮B的左侧位于按钮A的右侧;其他的方位都一样。我们在代码中的表示如下:

 <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        app:layout_constraintLeft_toRightOf="@id/btn1" />

其中最关键的属性为app:layout_constraintLeft_toRightOf="@id/btn1",代表按钮B的左侧位于按钮A的右侧

其中常用的属性还有:

 // 值可以为parent,意思就是A的左侧位于父布局的左侧(top_top|bottom_bottom等一样)
layout_constraintLeft_toLeftOf             
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf  在两个控件高度不一样时,可以保证文字对齐
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

引用官方的一张图来说明这些属性:
在这里插入图片描述

比较特殊的有layout_constraintBaseline_toBaselineOf,如图所示,两个Button的高度不一致,但是又希望他们文本对齐,这个时候就可以使用layout_constraintBaseline_toBaselineOf保证文字相对齐:
在这里插入图片描述

<Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Button3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:text="Button4"
        app:layout_constraintBaseline_toBaselineOf="@id/btn3"
        app:layout_constraintLeft_toRightOf="@id/btn3" />
2.2 角度定位

角度定位指的是利用角度以及距离决定某些控件中心点的坐标。
三个属性为:

  1. app:layout_constraintCircle="@id/btn3" 指定相对于哪个控件
  2. app:layout_constraintCircleAngle="160" 角度
  3. app:layout_constraintCircleRadius="150dp" 中心点的距离

其中的关系如下图所示:
在这里插入图片描述
例子:按钮5在按钮3的160度方向距离为150dp

<Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Button3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.3"
       
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Android ConstraintLayout是一种用于布局的容器,它可以帮助开发者更灵活地管理视图之间的关系和位置。在使用ConstraintLayout时,可以使用一些属性来控制视图的位置和约束关系。例如,可以使用app:layout_constraintDimensionRatio属性来设置视图的宽高比,使用app:layout_constraintLeft_toLeftOf和app:layout_constraintRight_toRightOf属性来约束视图的左右边界,使用app:layout_constraintTop_toTopOf和app:layout_constraintBottom_toBottomOf属性来约束视图的上下边界。\[1\]\[2\] 此外,还可以使用app:layout_constraintHorizontal_bias和app:layout_constraintVertical_bias属性来设置视图在水平和垂直方向上的偏移系数。\[2\] 需要注意的是,在使用ConstraintLayout时,如果没有正确设置视图的约束关系,某些属性可能会失效。例如,如果没有设置视图在布局中的位置约束,那么设置视图的边距属性可能不会生效。\[3\] 因此,在使用ConstraintLayout时,需要仔细设置视图的约束关系,以确保布局的正确性和效果的实现。 #### 引用[.reference_title] - *1* *2* [Android ConstraintLayout 详解及示例](https://blog.csdn.net/klylove/article/details/121967701)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v12^insert_chatgpt"}} ] [.reference_item] - *3* [Android——ConstraintLayout(约束布局)](https://blog.csdn.net/The_onion/article/details/127675500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v12^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值