Android中ConstraintLayout的基础知识和创建动画(1)

本文介绍了ConstraintLayout的基本概念,如何在AndroidStudio中使用它进行布局配置,包括依赖属性、居中对齐、基线对齐、Guideline和创建动画。重点讲解了其与RelativeLayout的区别以及关键属性的使用示例。
摘要由CSDN通过智能技术生成
  • 什么是ConstraintLayout

    • 简要介绍
  • 如何使用ConstraintLayout

    • 配置依赖
  • 基本属性

  • 用法示例

    • 居中
  • 偏移

  • 基线对齐

  • 实例

  • Guideline

  • Group

  • Barrier

  • 如何利用ConstraintLayout创建动画

    • 基本思想
  • 动画效果

  • 总结

什么是ConstraintLayout

==============================================================================

简要介绍


ConstraintLayout名为约束布局,如它的名字所示,ConstrainLayout可以通过约束一个控件相对于另一个控件的位置来得到你想要的布局。这么一说,似乎RelativeLayout也能做到,但是ConstraintLayout的功能远比RelativeLayout强,也正是因为如此,从 Android Studio 2.3 起,Android Studio官方的模板默认使用 ConstraintLayout。

如何使用ConstraintLayout

===============================================================================

配置依赖


要想用到ConstraintLayout首先要在app/grandle下添加ConstrainLayout依赖,如下(版本号根据需求自定):

dependencies {

compile ‘com.android.support.constraint:constraint-layout:1.0.2’

}

注:Android Studio 2.3以后自带这个包,不需要手动添加。

基本属性


前文说到RelativeLayout与ConstraintLayout有异曲同工之妙,ConstrainLayout的属性值可以是某个id,也可以是父组件"parent"。下面通过一个表格归纳ConstraintLayout与RelativeLayout的各种相似属性,当然ConstraintLayout的属性远不止这些,其余属性在后面会稍作介绍:

| ConstraintLayout属性 | RelativeLayout属性 |

| :-- | :-- |

| layout_constraintBaseline_toBaselineOf | layout_alignBaseline |

| layout_constriantBottom_toBottomOf | layout_alignBottom |

| layout_constraintBottom_toTopOf | layout_above |

| layout_constraintEnd_toEndOf | layout_alignEnd |

| layout_constraintEnd_toStartOf | layout_toStartOf |

| layout_constraintLeft_toLeftOf | layout_alignLeft |

| layout_constraintLeft_toRightOf | layout_toRightOf |

| layout_constraintRight_toRightOf | layout_alignRight |

| layout_constraintRight_toLeftOf | layout_toLeftOf |

| layout_constraintStart_toStartOf | layout_slignStart |

| layout_constraintStart_toEndOf | layout_toStartOf |

| layout_constraintTop_toTopOf | layout_alignTop |

| layout_constraintTop_toBottomOf | layout_below |

注:需要注意的是当属性的值为parent时,这些属性需要成对出现,否则起不到约束效果,所谓的成对指的是若约束了layout_constraintLeft_toLeft/RightOf 必要约束layout_constraintRight_toLeft/RightOf,bottom与Top同理。

用法示例


居中

居中包括在约定方向上居中和在整个布局居中即处于页面正中心。

在约定方向上的居中,这里举的是垂直居中即该组件处于页面高度的一半的位置:

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

在页面正中心的实现也一样,只不过多了水平方向上的约束

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

LTR布局方式下(一般情况下 Android Studio默认此布局),View开始部分就是左边,即Start和Left等价,End和Right等价,所以以上代码也可以写成

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

需要注意的是,有的语言目前为止还是按照从右往左的顺序来书写的,例如阿拉伯语,此时需要采用RTL布局,即Right等价于Start,Left等价于End,如果在布局的时候需要适配不同的国家,以下有个参数要设置

android:supportsRtl=“true”

偏移

偏移的实现主要有两种做法:

  1. 约束组件的相对位置后通过规定margin属性来实现,是通过规定具体的数值来约束偏移量的。

比如以下代码:

android:layout_marginTop=“30dp”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

此代码最终实现的效果是在处于页面正中心的前提下,通过设置marginTop属性的值,使得该组件与页面上方的距离为30dp,以下为示例图:

偏移

2. 通过规定layout_constraintHorizontal_bias属性的数值实现水平偏移,通过规定layout_constraintVertical_bias属性的数值实现垂直偏移。需要注意的是和margin不同,bias的值是一个比例值,是相对于布局的比例属性。

下面是layout_constraintHorizontal_bias的一个示例:

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintHorizontal_bias=“1”

app:layout_constraintStart_toStartOf=“parent”

此代码最终实现的效果是在水平居中后,偏向布局最右端。

以下为示例图:

水平偏移

下面通过一个表格来说明水平偏移中bias的值对偏移的影响:

| layout_constraintHorizontal_bias | 该组件位置 |

| — | — |

| 0 | 布局最左端 |

| 1 | 布局最右端 |

| 0.5 | 布局的水平居中处 |

| >0.5 | 偏向布局的水平靠右端 |

| <0.5 | 偏向布局的水平靠左端 |

垂直偏移量layout_constraintVertical_bias与上述水平偏移相似,不再赘述。

基线对齐

这个功能主要与layout_constraintBaseline_toBaselineOf这个属性有关,首先要知道什么是基线。基线通俗地来讲就是该组件的水平位置上一条居中的线。

基线未对齐:

在这里插入图片描述

基线对齐:

基线对齐

那么如何使两个组件的基线对齐呢,以下是示例代码

<TextView

android:id=“@+id/组件1”

…/>

<TextView

android:id=“@+id/组件2”

app:layout_constraintLeft_toRightOf=“@+id/组件1”

app:layout_constraintBaseline_toBaselineOf=“@+id/组件1”/>

实例

介绍了如上三点的主要内容后,接下来是一个结合了上面三点即居中,偏移,基线实现的小案例,以供参考:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<TextView

android:id=“@+id/red”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background=“#F00”

android:padding=“23dp”

android:text=“RED”

android:textColor=“#050500” />

<TextView

android:id=“@+id/org”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background=“#FFA600”

android:padding=“23dp”

android:text=“ORANGE”

android:textColor=“#050500”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintStart_toStartOf=“parent” />

<TextView

android:id=“@+id/yellow”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background=“#FFFF00”

android:padding=“23dp”

android:text=“YELLOW”

android:textColor=“#050500”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintHorizontal_bias=“1”

app:layout_constraintStart_toStartOf=“parent” />

<TextView

android:id=“@+id/blue”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“30dp”

android:background=“#0000FF”

android:padding=“23dp”

android:text=“BLUE”

android:textColor=“#FFFFFF”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

<TextView

android:id=“@+id/green”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginRight=“10dp”

app:layout_constraintBaseline_toBaselineOf=“@id/blue”

android:background=“#00FF00”

android:padding=“23dp”

android:text=“GREEN”

android:textColor=“#050500”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toStartOf=“@id/blue”

app:layout_constraintTop_toTopOf=“parent” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginLeft=“10dp”

app:layout_constraintBaseline_toBaselineOf=“@id/blue”

android:background=“#4A0084”

android:padding=“23dp”

android:text=“INDIGO”

android:textColor=“#FFFFFF”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintStart_toEndOf=“@id/blue”

app:layout_constraintTop_toTopOf=“parent” />

<TextView

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_marginLeft=“10dp”

android:layout_marginTop=“30dp”

android:background=“#EE82EE”

android:padding=“23dp”

android:text=“VIOLET”

android:textAlignment=“center”

android:textColor=“#050500”

app:layout_constraintBottom_toBottomOf=“parent” />

</android.support.constraint.ConstraintLayout>

实现效果:

效果

Guideline

Guideline 顾名思义是一条导航线,它的作用主要是分割布局。在LinearLayout中我们想要分割布局通常是使用设置权重的方式来分割,比如

android:layout_weight=“1”

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

光有这些思路和搞懂单个知识的应用是还远远不够的,在Android开源框架设计思想中的知识点还是比较多的,想要搞懂还得学会整理和规划:我们常见的**Android热修复框架、插件化框架、组件化框架、图片加载框架、网络访问框架、RxJava响应式编程框架、IOC依赖注入框架、最近架构组件Jetpack等等Android第三方开源框架,**这些都是属于Android开源框架设计思想的。如下图所示:

image

这位阿里P8大佬针对以上知识点,熬夜整理出了一本长达1042页的完整版如何解读开源框架设计思想PDF文档,内容详细,把Android热修复框架、插件化框架、组件化框架、图片加载框架、网络访问框架、RxJava响应式编程框架、IOC依赖注入框架、最近架构组件Jetpack等等Android第三方开源框架这些知识点从源码分析到实战应用都讲的简单明了。

由于文档内容过多,篇幅受限,只能截图展示部分,更为了不影响阅读,这份文档已经打包在GitHub,有需要的朋友可以直接点此处前往免费下载

image

image

整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~

、RxJava响应式编程框架、IOC依赖注入框架、最近架构组件Jetpack等等Android第三方开源框架,**这些都是属于Android开源框架设计思想的。如下图所示:

[外链图片转存中…(img-qKaDxIUV-1710507062957)]

这位阿里P8大佬针对以上知识点,熬夜整理出了一本长达1042页的完整版如何解读开源框架设计思想PDF文档,内容详细,把Android热修复框架、插件化框架、组件化框架、图片加载框架、网络访问框架、RxJava响应式编程框架、IOC依赖注入框架、最近架构组件Jetpack等等Android第三方开源框架这些知识点从源码分析到实战应用都讲的简单明了。

由于文档内容过多,篇幅受限,只能截图展示部分,更为了不影响阅读,这份文档已经打包在GitHub,有需要的朋友可以直接点此处前往免费下载

[外链图片转存中…(img-DFqaQrgL-1710507062957)]

[外链图片转存中…(img-wXLWnNrC-1710507062958)]

整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~

你的支持,我的动力;祝各位前程似锦,offer不断!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值