Android-ConstraintLayout用法

一、简介

ConstraintLayout具有很好的性能优势,可以绘制扁平化界面,减少复杂界面造成的布局嵌入过深的问题,经常使用一层嵌套便可以实现界面。

ConstraintLayout是使用比例方式进行的排版布局,所以可以很好的适配各种大小和分辨率的手机屏幕。

二、使用

1.如何在代码中引入ConstraintLayout

新建一个布局文件可以看到,Google已经默认帮我们选择好了父布局为ConstraintLayout。

 2.ConstraintLayout布局的约束属性

ConstraintLayout共给我们提供了13种属性,通过属性的意思便能知道属性的具体含义。

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 

下面通过以上属性实现一个九宫格界面布局的例子,直接放代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ...>

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="@id/tv_4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tv_2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toTopOf="@id/tv_5"
        app:layout_constraintLeft_toRightOf="@id/tv_1"
        app:layout_constraintRight_toLeftOf="@id/tv_3"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toTopOf="@id/tv_6"
        app:layout_constraintLeft_toRightOf="@id/tv_2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toTopOf="@id/tv_7"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tv_5"
        app:layout_constraintTop_toBottomOf="@id/tv_1" />

    <TextView
        android:id="@+id/tv_5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toTopOf="@id/tv_8"
        app:layout_constraintLeft_toRightOf="@id/tv_4"
        app:layout_constraintRight_toLeftOf="@id/tv_6"
        app:layout_constraintTop_toBottomOf="@id/tv_2" />

    <TextView
        android:id="@+id/tv_6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toTopOf="@id/tv_9"
        app:layout_constraintLeft_toRightOf="@id/tv_5"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_3" />

    <TextView
        android:id="@+id/tv_7"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tv_8"
        app:layout_constraintTop_toBottomOf="@id/tv_4" />

    <TextView
        android:id="@+id/tv_8"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/tv_7"
        app:layout_constraintRight_toLeftOf="@id/tv_9"
        app:layout_constraintTop_toBottomOf="@id/tv_5" />

    <TextView
        android:id="@+id/tv_9"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/tv_8"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_6" />


</androidx.constraintlayout.widget.ConstraintLayout>

上述约束实现的布局效果如下:

3.ConstraintLayout布局居中

ConstraintLayout可以根据比例进行页面的调整,用到的属性有以下几个:

layout_constraintHorizontal_bias //水平方向居中时左右边距的比例
layout_constraintVertical_bias //垂直方向居中时上下边距的比例

通过一个例子可以很直观的看到: 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ...    >

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        ...
        app:layout_constraintBottom_toTopOf="@id/tv_2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        ...
        app:layout_constraintHorizontal_bias="0.25"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_1"
        app:layout_goneMarginBottom="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

运行结果:

4.角度定位

 ConstraintLayout可以通过三个属性,实现按照中心点控件进行的角度定位。

app:layout_constraintCircle //约束以谁为圆点
app:layout_constraintCircleRadius //约束角度的距离
app:layout_constraintCircleAngle //约束的角度

 以下代码为让蓝色圆圈围绕红色圆点顺时针旋转123度:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ...>

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="100dp"
        android:layout_height="100dp"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintCircle="@id/tv_center"
        app:layout_constraintCircleAngle="123"
        app:layout_constraintCircleRadius="200dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

运行结果:

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当我们在进行Android布局时,经常会遇到复杂的UI界面,这时候使用ConstraintLayout就可以很好地解决这个问题,因为它是最灵活的布局方式。下面我为您详细讲解一下ConstraintLayout用法。 首先,我们需要在build.gradle文件中添加依赖项: ``` implementation 'com.android.support.constraint:constraint-layout:1.1.3' ``` 然后,在XML布局文件中,我们需要将根布局更改为ConstraintLayout,如下所示: ``` <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> ... </androidx.constraintlayout.widget.ConstraintLayout> ``` 接下来,我们可以使用以下属性来设置视图的位置和大小: - layout_constraintLeft_toLeftOf:将视图的左边缘与另一个视图的左边缘对齐。 - layout_constraintRight_toRightOf:将视图的右边缘与另一个视图的右边缘对齐。 - layout_constraintTop_toTopOf:将视图的顶部边缘与另一个视图的顶部边缘对齐。 - layout_constraintBottom_toBottomOf:将视图的底部边缘与另一个视图的底部边缘对齐。 - layout_constraintStart_toStartOf:将视图的开始边缘与另一个视图的开始边缘对齐。 - layout_constraintEnd_toEndOf:将视图的结束边缘与另一个视图的结束边缘对齐。 - layout_constraintBaseline_toBaselineOf:将视图的基线与另一个视图的基线对齐。 - layout_constraintWidth_percent:将视图的宽度设置为父视图宽度的百分比。 - layout_constraintHeight_percent:将视图的高度设置为父视图高度的百分比。 除此之外,还有一些其他的属性,例如layout_constraintHorizontal_bias、layout_constraintVertical_bias、layout_constraintDimensionRatio等。 最后,我们需要使用以下属性来设置视图的约束: - layout_constraintLeft_creator - layout_constraintTop_creator - layout_constraintRight_creator - layout_constraintBottom_creator - layout_constraintStart_creator - layout_constraintEnd_creator 总的来说,使用ConstraintLayout可以让我们更轻松地创建复杂的UI布局。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值